0 votes

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
    else:
        material.albedo_color = defcolor
in Engine by (100 points)
edited by

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?

1 Answer

0 votes

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

by (148 points)

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.

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.