- Home /
Childs material
Hello,
I have an empty game object, which has several child game objects. I would like to change the textures of some of the childs (only those with the name "platform") to a new one.
How should I do it by script?
Thanks!
Comment
Best Answer
Answer by GuyTidhar · Aug 31, 2011 at 01:11 PM
var yourTexture : Texture;
// Look for all renderers in the child of the current game object (the object to
// which you attach this script)
var renderers : Renderer[] = gameObject.GetComponentsInChildren.<Renderer>();
if ( renderers )
{
// Iterate through all renderers
foreach(var rend:Renderer in renderers)
{
// Is the game object this renderer is attached to is named "platform"?
if ( rend.name == "platform" )
{
foreach(var mat:Material in rend.materials)
{
mat.SetTexture("_MainTex", yourTexture);
}
}
}
}
Also, Check out some types of textures you can replace: http://unity3d.com/support/documentation/ScriptReference/Material.SetTexture.html
Your answer
Follow this Question
Related Questions
Change/load a mesh texture 2 Answers
texture change problem 1 Answer
Flickering materials/textures when changed by script 1 Answer
Change quality of all textures of the same type 0 Answers
How can I change an objects texture through javascript? 1 Answer