- Home /
How to instantiate a prefab and change its color?
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 sommmen · Mar 21, 2016 at 09:43 AM
I know that this is an old question - but after spending 3 hours I have found the answer. It appears that unity compacts the 'material' of an instantiated prefab under the meshrenderer rather than an attached 'material' component.
To acces the material component use (for ex.):
prototype.GetComponent<MeshRenderer>().material.SetColor("_Color",Color.red);
where prototype is the instantiated prefab.
I came to this conclusion after printing out all components and missing the material component. Turns out i could acces it through the meshrenderer instead.
This is one of those things that "everyone" knows. In a standard modeling program, each mesh and submesh has it's own material, possibly several. That's why the Unity Inspector shows $$anonymous$$aterials as a list inside the Renderer component. Anyone with a 3D modelling background knows that what looks like a $$anonymous$$aterial component has to be merely a shortcut to the 1st material on the first submesh.
I mention this because Unity borrows so much from just the regular way people make games. So you can often learn about more about how Unity works by reading the standard non-Unity 3D modeling, texturing, shading, animation methods.
Answer by Charfon · Nov 23, 2012 at 01:27 PM
To instantiate a prefab :
var myNewObject = Instantiate (prefab, Vector3(x, y, z), Quaternion.identity);
To change color :
myNewObject.renderer.material.color = Color.red;
Note: If you change the color like this, Unity will create a new material for your object. $$anonymous$$eaning dynamic batching won't work any more!
Answer by Charfon · Nov 23, 2012 at 01:27 PM
be sure to add a name while instantiating the object with :
myNewObject.transform.name = "Head";
This isn't needed, if you don't specify a name it'll just be "PrefabName (Clone)"
Answer by GeorgeRigato · Nov 23, 2012 at 01:28 PM
Do the parts have separated materials? If not, you must have a material for each part you want to manipulate.
Basically you have to have a reference from instantiation, and I think you achieved this.
Then you do:
partReference.renderer.material.color = Color.red; //Insert the color you want.
Or, if you instantiated a prefab named "Head", maybe it actually is called "Head(Clone)".
Good luck!
Answer by tylerwongj · Mar 12, 2019 at 11:31 PM
I actually had to use sharedMaterial
:
prototype.GetComponent<MeshRenderer>().sharedMaterial.SetColor("_Color",Color.red);
Your answer
Follow this Question
Related Questions
GUI button texture different color when script is on Instantiated object 0 Answers
change the colour of mass placed tree leaves (or make them transparent) 1 Answer
Why is instantiated animator prefabs are not working properly? 2 Answers
How would I change the colour of a prefab relative to the size of the character? 1 Answer