Error while changing mesh albedo color with code

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Harshitsoam

Hi , So I am am trying to change my enemy mesh color when player enters in Enemy’s area
here is the code - `$EnemyMesh.get_surface_material(0).set_albedo(Color(1,0,0))’

I have a spatial material on this $EnemyMesh

Here is the tutorial which I am following -

In this video code properly worked and mesh changed its color but when I does the same thing I got this awkward type of error

Attempt to call function ‘set_albedo’ in base ‘null instance’ on a null instance.

Here is my node hierarchy of enemy scene -

It is saying that $EnemyMesh is null instance whereas it is in node hierarchy. I don’t know why it is not working for me but working in this tutorial which is given above.

Please anyone help me
It’s much appreciated

What node in your scene tree contains the above script code? The reference of $EnemyMesh assumes the script is on the Enemy node (according to the tree you’ve outlined above).

jgodfrey | 2022-08-15 15:21

This script is in $Enemy node which is a part of a main scene tree and the given hierarchy is also of $Enemy node

Harshitsoam | 2022-08-15 15:49

:bust_in_silhouette: Reply From: Inces

It is not EnemyMesh that is missing, but its material.
You must have misled the reference. Spatial nodes actually have 3 different paths to materials. Mesh Surfaces are just one of them. Another one is Spatial.mesh.material and GeometryInstance.material_overrride.
Examine where in these properties your actual imported mesh resides.

Bro I don’t got it how can I examine which property my actual mesh resides. And what do do after that? Please explain (sorry I am a complete beginner) :frowning:

Harshitsoam | 2022-08-15 16:46

There are three properties of Mesh Instance, that can have material.
Each of them can be found in objects tab in editor, the big one aligned to the right . Their names are as stated below

  1. MeshInstance → Material.
    If You click this one, You will see numbers. If your mesh was imported by default, there will only be number 0. This means it is surface number 0.

2.MeshInstance → Mesh → Material
3.GeometryInstance → Geometry → Material_override

Your code can only work if there is any material in surface nr 0. Go to this property in editor, through names I shown in 1st paragraph, and create new material there.
Alternatively, You can create a new material in any of the other properties from paragrapgh 2 and 3, and change your code to lead to its path.

Inces | 2022-08-15 19:06

Thanks bro so much may god bless you :slight_smile: I have my material in 2 paragraph (MeshInstance-Mesh-Material) but when I changed it to paragraph 1 (MeshInstance-Material) It worked :wink:

Harshitsoam | 2022-08-16 16:10