This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

Hi , So I am am trying to change my enemy mesh color when player enters in Enemy’s area
here is the code - `$EnemyMesh.getsurfacematerial(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

Godot version v3.4.4
in Engine by (29 points)
edited by

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).

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

1 Answer

+1 vote
Best answer

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.

by (8,188 points)
selected by

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) :-(

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.

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.