How do I set the texture on a child object of an instantiated prefab object?
How do I set the texture on a child object of an instantiated prefab object? I can't seem to find a way to access the child object's components, and the only suggestion I've found on here is to access the child object as a Transform, which I then don't appear to be able to GetComponents from.
If I get the object in question, then calling "...transform.GetChild(0);" gets the child object, but returns a Transform rather than a GameObject. I don't understand why... a Transform apparently only deals with the position, rotation and location of the GameObject. I need to set the texture on this "Transform"... Any help welcome!
Nick
All parent-child relationships are specified by the transform hierarchy. You can access children through transform.children
, or helper functions like transform.Find()
, or you can grab the components directly with functions like GetComponentsInChildren()
.
the only suggestion I've found on here is to access the child object as a Transform, which I then don't appear to be able to GetComponents from.
Why not? The child is just another GameObject, so you should be able to look up its components if it has any. Are you seeing some error message when you try?
O$$anonymous$$, I'll try that.
When I looked up Transform in the help, it says "It's used to store and manipulate the position, rotation and scale of the object." Why does this object contain links to the children? Seems counter-intuitive at best.
I can't access transform.children. It doesn't exist, apparently. If I get the object in question, then calling "card.transform.GetChild(0);" gets the child object, but returns a Transform rather than a GameObject. I don't understand why... a Transform apparently only deals with the position, rotation and location of the GameObject. I need to set the texture on this "Transform"...
Answer by jonofarc · Nov 10, 2015 at 08:29 PM
In this case you just need to specify that you want the GameObject from the Transform it would be like
card.transform.GetChild(0).gameObject;
This will get you the child gameObject
then you just have to set the texture
card.transform.GetChild(0).gameObject.GetComponent().mainTexture = "Texture Goes Here";
or
GameObject MyGameObject =card.transform.GetChild(0).gameObject;
MyGameObject.GetComponent().mainTexture= "Texture Goes Here";
I assume I should be using GetComponent() with the type of $$anonymous$$eshRenderer ? (This works! Thanks! )
Answer by Statement · Nov 10, 2015 at 08:37 PM
On your instantiated clone, get the transform of the child, then get the renderer of that child transform.
// Create a prefab, and get the nail child of the clone
var clone = Instantiate(prefab);
var nail = clone.transform.Find("Left Arm/Hand/Pinky/Nail");
// Ok, not sure why I picked a stupid example,
// but get the renderer on that nail and change its
// renderers materials texture to fancyPolish.
var nailRenderer = nail.GetComponent<Renderer>();
nailRenderer.material.mainTexture = fancyPolish;
// (It's stupid, because it's stupid to have a renderer
// on each nail, btw, imo. But that doesn't affect the
// example otherwise)
If it fails, either you have a typo in your child name, path, or it doesn't have a renderer.
Your answer
