- Home /
How can I add a MeshRenderer component with all properties to a GameObject by script ?
void Start()
{
GameObject levels = GameObject.Find("_Level");
foreach (Transform child in levels.transform)
{
child.gameObject.AddComponent<MeshRenderer>();
}
}
But this will give me the result of:
In the Inspector I have now Mesh Renderer (Script) But I want to have the Mesh Renderer component with all the properties.
And then I want also to add a material with color to the object for example I want to color the object in Red.
How can i add the mesh renderer component with all properties ?
How can i color/un color the object ?
What I mean mesh renderer component with properties is for example this:
Then I want to be able to color and un color the object.
Answer by Bunny83 · Jan 20, 2018 at 04:55 AM
You most likely created a script and named it MeshRenderer. This will "hide" Unity's MeshRenderer component. Unity's MeshRenderer is defined in the UnityEngine namespace. However if you create a class in the global namespace with the same name it takes precedence.
So the best solution is to rename / delete your own MeshRenderer class. However if you really want to keep it you would have to use:
child.gameObject.AddComponent<UnityEngine.MeshRenderer>();
In order to use the correct component.
Your answer
Follow this Question
Related Questions
Why when changing the doors colors it's not changing anything ? 1 Answer
How can i List objects by name but also in small text or big text or any kind ? 1 Answer
How can i add a button to the inspector ? 2 Answers
How can i rotate a transform using lerp but keep the nose in same direction/angle on x ? 1 Answer