0 votes

I have some code that changes the color of the player when I press a button:

var color = SpatialMaterial.new()

func _on_Blue_pressed():
    color.albedo_color = Color(0, 0, 1)
    $MeshInstance.set_surface_material(0, color)

But when I change the scene it resets the color.

I know the basics of how singletons work but I have no idea how to keep the color the same using them. I assume I would need to change the player's color in the singleton's script but I cannot figure out how to do that, does anyone know how I can do this?

Godot version 3.3
in Engine by (15 points)

1 Answer

+1 vote
Best answer

You can store the color of the material in the Singleton, like player_color or something. After reloading the scene, you can fetch the color from the Singleton and set it for the material.

by (8,150 points)
selected by

I have tried this but I don't know how I would get the player's material.
And how would I detect when the scene is changed or reloaded?

I figured out how to get the player's material and set it using the singleton but I have no idea how I would know when the scene is changed.

The ready function will be called again when the scene is reloaded

So the _ready() function will be called every time the scene changes if it is on the singleton?

No, the _ready function will be called on the scene that was reloaded/changed.

I think I get it now, 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 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.