Invalid access to property or key ´modulate´ with value of type ´Color´ on base object of type ´null instance

Godot Version

4.3.

Question

I have a function that should make the sprite blink red every time the unit takes damage, but for some reason the massage “Invalid access to property or key ´modulate´ with value of type ´Color´ on base object of type ´null instance`”

func take_damage (damage_to_take):
	health-= damage_to_take
	
	if health <=0:
		queue_free()
	
	sprite.modulate = Color.RED
	await get_tree().create_timer(0.1)
	sprite.modulate = Color.WHITE

I have variable assigned to Sprite2D like so:
var sprite : Sprite2D

I am pretty new in all this, so this is probably a pretty easy thing, but I just don’t figure the error out.

Many thanks for any kind of help.

Are you calling this before node is ready?
When initializing scene, each node first processes its children and itself and only then are things that are needed at runtime (node references in this case) available.

If this is the case, you might need to await that ready signal:

if not is_node_ready():
		await self.ready

Note that self.ready can be replaced with just ready

2 Likes

In this line, you are declaring the variable, but not assigning a value to it. Basically you’re telling the program “I will have a variable that is a Sprite2D”, but you are not giving this variable a value, so it defaults to null.

Did you assign an instantiated Sprite2D value to that value before calling sprite.modulate?

2 Likes

Many thanks for replying.

I call the function through the _process(delta) function through other functions, as far as my, very limited, understanding goes, that should be enough to call the function, isn’t that so?

Thanks for the reply.

The Sprite2D is instantiated.

I have basically copied the code form a tutorial I am following. No value had to be assigned to var sprite : Sprite2D and in the video everything was running fine.
Could it be that things have changed lately?

var sprite : Sprite2D

That’s like saying “this sprite is a square hole,” but you have to put the square peg into it.

There has to be more to this. Make sure you didn’t miss something.

I don’t know if this worked differently in previous versions of Godot, but i am sure that you would get an error if you tried to access a property of an node with no value (null) in the current version.

Perhaps in the tutorial they assigned a value to the sprite variable through the inspector tab and not through code. if you change

var sprite : Sprite2D

to

@export var sprite: Sprite2D

that property will be displayed in the inspector (on the right side of the editor, by default) and you’d be able to select a sprite node from there to have that value assigned outside of code.
Again, i don’t know if the “@export” tag was needed for this in previous versions of Godot

1 Like

Thanks a lot! apparently that did the tick :man_shrugging:

Jumping in here because I’m doing the same tutorial and ran into the same issue as OP.

I copied in the tutorial project and it works. sprite is defined in the _ready function

func _ready ():
	agent = $NavigationAgent2D
	sprite = $Sprite

I’m certain OP has this as well. My code matches the tutorial code, the tutorial game works, mine does not.

What would be missing that doesn’t allow sprite to get a value?

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.