- Home /
Question by
globalenemy · Oct 25, 2015 at 12:24 AM ·
scripting problem
how do I edit a component of a specific child of an instantiated object?
I'm trying to instantiate a new GameObject from a prefab and change the material of a child GameObject. I found several answered Questions here, but the answers do not seem to work for me, so please help me out. :(
GameObject newCharacter = Instantiate(character, spawnPosition, spawnRotation) as GameObject;
The child GameObject is called "Body". My target is to either swap the associated matierial with another one or change some details of it, like the albedo color.
Comment
note, that I will instantiate multiple characters over time, so there will be more than one object in the scene called "Body".
So your prefab is instantiating a gameobject whose child will be body?
Best Answer
Answer by MrMeows · Oct 25, 2015 at 04:14 AM
MeshRenderer bodyRenderer = newCharacter.transform.Find("Body").GetComponent[MeshRenderer]();
Replace those square brackets with angle brackets. Unlike GameObject.Find, Transform.Find looks at only children of the Transform in question.
Answer by AshleyJamesy · Oct 25, 2015 at 05:05 PM
GameObject m_Object = gameObject.transform.FindChild("child").gameObject;
Transform m_Transform = m_Object.GetComponent<Transform>();
Your answer
