Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Tro1ka · Jun 07, 2013 at 06:43 PM · modelfbxrender

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");
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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")....

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Tro1ka · Jun 07, 2013 at 09:44 PM 0
Share

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?

avatar image Owen-Reynolds · Jun 08, 2013 at 03:36 AM 0
Share

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.

avatar image Tro1ka · Jun 12, 2013 at 02:54 PM 0
Share

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.

avatar image Owen-Reynolds · Jun 12, 2013 at 06:04 PM 0
Share

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.

avatar image Tro1ka · Jun 13, 2013 at 06:45 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges