0 votes

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
in Engine by (260 points)

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

1 Answer

0 votes

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 :P (depends on the design, you may want a tool or custom node).

by (7,860 points)

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

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 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 webmaster@godotengine.org with your username.