Changing color of new created instances

Godot Version

4.2.1

Question

whene i try to change the color of mesh of new created instances
the color of ALL instances change to new color

	var instance = load("res://scenes/path_follow_3d.tscn").instantiate()
	var mess:CapsuleMesh = instance.get_children()[0].get_mesh()
	var material = mess.surface_get_material(0)

	material.albedo_color = Color(RandomNumberGenerator.new().randf_range(0.0,1), 0, 0)
	mess.surface_set_material(0, material)
	instance.get_children()[0].set_mesh(mess)
	$Path3D.add_child(instance)

Materials are Resources. Resources are shared by default. You’ll need to make them unique doing one of the following:

  • Right click over it in the inspector and click on Make Unique
  • Enable Local to Scene in the Resource. This will make unique the resource when instantiating the scene (if the resource is shared between nodes in the same scene the copies won’t be made unique)
  • Calling Resource.duplicate() in code.

In your case, it would be something like:

	var instance = load("res://scenes/path_follow_3d.tscn").instantiate()
	var mess:CapsuleMesh = instance.get_children()[0].get_mesh()
	var material = mess.surface_get_material(0)
	
	# Duplicate the material
	material = material.duplicate()

	material.albedo_color = Color(RandomNumberGenerator.new().randf_range(0.0,1), 0, 0)
	mess.surface_set_material(0, material)
	instance.get_children()[0].set_mesh(mess)
	$Path3D.add_child(instance)
1 Like

thnak you very much fpr the fast and good solution !!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.