Attempt to call function 'set modulate' in base 'null instance' on a null instance.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By lavaduder
:warning: Old Version Published before Godot 3 was released.

I’m trying to make a block’s color editable in the staticbody2d. Why? Because I’m exporting it to another scene. Where I can easily have multicolored blocks with one scene. instead of having to make a different scene color for each and every block.

However for some odd reason i get this message.
“Attempt to call function ‘set modulate’ in base ‘null instance’ on a null instance.”
I don’t understand why I am getting it. Since this isn’t calling a different scene.

Can someone please explain?

extends StaticBody2D

export var brickcolor = Color() setget brick_modulate

func brick_modulate(value):
	brickcolor = value
	get_node("block").set_modulate(brickcolor)
	#block is a sprite node inside the StaticBody2D

Is there a child named “block”? (look at the remote inspector when error occurs)
get_node is returning null.

eons | 2017-03-23 18:37

:bust_in_silhouette: Reply From: eons

Just realized the problem here, variables are initialized when instancing, a moment where your node does not even have a child, you need to wait for _ready or _enter tree.

A solution could be setting the modulate on ready, a better looking solution will be making a tool, but an easier solution, since static bodies do not move (or move by set_pos), is to make the sprite as the root :stuck_out_tongue: (depends on the design, you may want a tool or custom node).

Okay I’m a beginner, I did not know what a tool was.
and thanks to this dude https://www.youtube.com/watch?v=vsxB4L6rufo
I found out what I had to do

tool
extends StaticBody2D
export var brickcolor = Color() setget brick_modulate

func brick_modulate(value):
	brickcolor = value
	if get_node("block") != null:
		get_node("block").set_modulate(brickcolor)

There now I can change the color Without issues. Thanks

lavaduder | 2017-03-24 07:14