Reputation: 228
I have a simple character scene that I instance several times in the main level scene. It's a kinematic body with a meshinstance and a collision shape as children. I've attached the following script to the scene, with the intent that I could easily change the body color of a character in the editor.
tool
extends KinematicBody
const char_material = preload("res://Materials/Character_material.tres")
export var body_color: Color setget change_color
func change_color(new_color: Color):
var new_material = char_material
new_material.albedo_color = new_color
$BodyMesh.material_override = new_material
body_color = new_color
This script works as expected, except that when I change the color of one character all characters change color. What's the best way to change the color of only one character?
Upvotes: 1
Views: 1737
Reputation: 40315
All the instances of this KinematicBody are getting the same char_material
, which you modify in change_color
. Duplicate the material before modifying it:
var new_material := char_material.duplicate()
Upvotes: 1