- Home /
Beginner: assigning texture to part of GameObject
I have a GameObject originated from a FBX file - representing a 3D object. It is combined from several 3D parts. I have materials (2D textures imported from JPG) for these parts. In Unity editor I am able to drag&drop these materials onto appropriate parts. But I can't find a way how to do this in a script.
Following code
Texture2D tex = (Texture2D)Resources.Load("Materials/xxx", typeof(Texture2D));
go.renderer.material.mainTexture = tex;
set the texture to the GameObject itself and not to the appropriate part.
What I'm missing is what are these parts in context of Unity and hot to access them. Thanks
Answer by Owen-Reynolds · May 10, 2011 at 07:29 PM
So, you are seeing the parts as children of a main game object? To access kids, take a look at Transform.Find in the manual: http://unity3d.com/support/documentation/ScriptReference/Transform.Find.html. Ex (this is C#):
Transform arm = transform.Find("arm"); Transform foot = transform.Find("body/leg/foot");
arm.renderer.material.color = Color.red; // null exception means it was spelled wrong, above
GameObjects don't have a Find, so if your GO is one, I think: go.transform.Find(.....)
Your answer
Follow this Question
Related Questions
Assigning meshes & materials from Assets, how? 1 Answer
Assigning variables in inspector 1 Answer
Assign material and guiskin to variables 1 Answer