(Answered) How to change variables for a specific instance of a node, instead of changing it for every instance.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By dari0us

Been stuck on this all week. Im very new to godot and its scripting language.

I have a raycast node, that is supposed to change the color of a ball when its colliding.
when i duplicate the ball, it is only unique in its transforms, and upon raycast collision, all the present balls change color, instead of the specific one that its colliding with, which is not what im after.

heres the code:

extends Spatial

var defcolor = Color.black
var eqcolor = Color.green
var selcolor = Color.white
onready var mesh = $RigidBody/MeshInstance
onready var col = $RigidBody/CollisionShape
onready var rig = $RigidBody
onready var raycast = get_node("/root/World/Player/Camera/RayCast")
onready var material = mesh.get_surface_material(0)

func _ready():
	material.albedo_color = defcolor

func _process(delta):
	if raycast.get_collider() == rig:
		material.albedo_color = selcolor
		material.albedo_color = defcolor


I believe your material is not private to a certain ball instance, instead it is common to all. Maybe try to create a new material in _ready for every instance and keep a reference to it?

grok | 2020-10-05 17:33

:bust_in_silhouette: Reply From: miskotam

All Resources (including Materials) are shared amongst instances. If you want to update a single instance’s color you need to create a new, unique Material.

newmaterial = material.duplicate()
newmaterial.albedo_color = defcolor
gmesh.set_surface_material(0, newmaterial)

An alternative way to make resources unique is using the “Local to Scene” property.
enter image description here

Thank you so much! Turning on Local To Scene fixed and answered so many issues i thought i had. I was about to give up on godot, because this had a huge impact on my progression. turns out it was just my lack of knowledge.
I honestly didnt expect to get any answers either. Proud to be a part of this community.

dari0us | 2020-10-05 20:22