- Home /
Change the Main Color of a FBX model with Script
Hi!
I'm trying to change de main color, or the "Element 0" of an FBX model of a car when i press a button in the game.
The problem i have, is that i don't know how to refer to that parameter in order to change it. Maybe i'm doing it the wrong way, because i try to use renderer.material.color but my model doesnt have a renderer attach to it so i always get the error that my game object doesnt have a renderer, but parts of that model has it (Like de door, the hood, etc).
Is there any way to refer to the car's part for changing the colors of it? Like when you want to refer to the game object you do:
var car : GameObject;
car = GameObject.Find("porche");
Answer by Owen-Reynolds · Jun 07, 2013 at 08:00 PM
If the door, hood, etc... are child objects, just use the trick to find children: GameObject.Find("car").transform.Find("hood").renderer;
The docs are http://docs.unity3d.com/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html
That includes the trick for dragging the car into the Inspector, so you can just say pinto.Find("door")...
.
Thanks for the quick response. I try it to do it how you indicated, but it didnt work. I was getting the "You cant mix transform and renderer" error.
Ins$$anonymous$$d, i created diferent game object variables for each part of the car, and it works perfectly, but is a real mess and it's complicated to used with more than one model. I'll keep reading the documentation to see what i did wrong.
There's other thing i can't work out, i'm trying to deactivate the renderer of the car, so i can upload a diferent car, but i have the same problem as before, too many parts, too many variables. Is there a way to work all the car parts as an array?
A common trick is to group all parts as the only children of a stub object. Then use the foreach(Transform tt in ???)
trick to go through them all. But that uses some of the same "find my children" math that you were having trouble with.
What i finally did was to tag all the car parts that i want to change the color and use FindGameObjectsWithTag to have an array of all of them. But i get the following error: "NullReferenceException: Object reference not set to an instance of an object". I know the array is full with the parts, but i think i only got the names of the parts, not the reference to the object.
FindObjects does give a reference to the actual gameObject (you can tell since it returns an array of gameObjects, not strings, which would be the case if it just got names.)
Try some Debugs to see where it fails (item0? just past end?) It's probably a regular array mistake.
One problem with the tagging approach is if you ever have two cars. $$anonymous$$akes it tough to get all parts for just one.
I finally made it work, i don't know what was'nt working before, but when i write the code again, it work. Now i can sleep.
if (GUI.Button (Rect (40,55,60,20), "Red", color1)) {
aParts = GameObject.FindGameObjectsWithTag("colourchange");
for (var go : GameObject in aParts){
go.renderer.material.color = Color.red;
}
}
There is the code if any one will need it. Just tag all the parts of the model that you want the color change and you got it.
Thanks for your help Owen.
Your answer

Follow this Question
Related Questions
Rendering FBX with multiple animations 1 Answer
Broken animation in URP project. 2 Answers
Best way to add corn stalks? 2 Answers
Unity FBX Export Nesting Issue 1 Answer
Model isn't displayed correctly 0 Answers