- Home /
Instantiated prefab color change
I am having a problem with prefabs I have instantiated with script. I can instantiate the prefab well enough but I seem to be having trouble with finding the parts of the prefab so that i can change their color. For exalmple once i have instantiated a character and I search for its head by using code like var head = GameObject.Find("Head"); and then when I try to change the color of var head I get a NullReference Exception. This only happens with instantiated prefabs. Can anyone help me? Thank You.
Answer by TheDarkVoid · Nov 23, 2012 at 03:28 PM
You should be modifying the renderer.material.color
Answer by Owen-Reynolds · Nov 23, 2012 at 05:05 PM
`GameObject.Find` doesn't care about the item you just spawned. It will search for any "Head" object. Use `Transform.Find` to search inside a particular object. Make sure you find the gameObject with the model on it (for example, here I pretend head has a headModel child.) Rough ex:
var newThing = Instantiate(...);
newHead = newThing.transform.Find("head/headModel");
// Assuming head is an empty, with real model below it
Then probably `head.renderer.material.color = ...` unless you used a really odd shader.