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
2
Question by tnyadams · Nov 23, 2012 at 12:39 PM · instantiateprefabcolorchange

How to instantiate a prefab and change its color?

I am having a problem with prefabs I have instantiated with script. I can instantiate the prefab well enough but I seem to be having trouble with finding the parts of the prefab so that i can change their color. For exalmple once i have instantiated a character and I search for its head by using code like var head = GameObject.Find("Head"); and then when I try to change the color of var head I get a NullReference Exception. This only happens with instantiated prefabs. Can anyone help me? Thank You.

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

5 Replies

· Add your reply
  • Sort: 
avatar image
5

Answer by sommmen · Mar 21, 2016 at 09:43 AM

I know that this is an old question - but after spending 3 hours I have found the answer. It appears that unity compacts the 'material' of an instantiated prefab under the meshrenderer rather than an attached 'material' component.

To acces the material component use (for ex.):

 prototype.GetComponent<MeshRenderer>().material.SetColor("_Color",Color.red);

where prototype is the instantiated prefab.

I came to this conclusion after printing out all components and missing the material component. Turns out i could acces it through the meshrenderer instead.

Comment
Add comment · Show 1 · 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 Owen-Reynolds · Mar 21, 2016 at 02:15 PM 0
Share

This is one of those things that "everyone" knows. In a standard modeling program, each mesh and submesh has it's own material, possibly several. That's why the Unity Inspector shows $$anonymous$$aterials as a list inside the Renderer component. Anyone with a 3D modelling background knows that what looks like a $$anonymous$$aterial component has to be merely a shortcut to the 1st material on the first submesh.

I mention this because Unity borrows so much from just the regular way people make games. So you can often learn about more about how Unity works by reading the standard non-Unity 3D modeling, texturing, shading, animation methods.

avatar image
0

Answer by Charfon · Nov 23, 2012 at 01:27 PM

To instantiate a prefab :

 var myNewObject = Instantiate (prefab, Vector3(x, y, z), Quaternion.identity);

To change color :

 myNewObject.renderer.material.color = Color.red;
Comment
Add comment · Show 1 · 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 The-Oddler · Nov 23, 2012 at 02:07 PM 0
Share

Note: If you change the color like this, Unity will create a new material for your object. $$anonymous$$eaning dynamic batching won't work any more!

avatar image
0

Answer by Charfon · Nov 23, 2012 at 01:27 PM

be sure to add a name while instantiating the object with :

 myNewObject.transform.name = "Head";
Comment
Add comment · Show 1 · 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 The-Oddler · Nov 23, 2012 at 02:06 PM 0
Share

This isn't needed, if you don't specify a name it'll just be "PrefabName (Clone)"

avatar image
0

Answer by GeorgeRigato · Nov 23, 2012 at 01:28 PM

Do the parts have separated materials? If not, you must have a material for each part you want to manipulate.

Basically you have to have a reference from instantiation, and I think you achieved this.

Then you do:

partReference.renderer.material.color = Color.red; //Insert the color you want.

Or, if you instantiated a prefab named "Head", maybe it actually is called "Head(Clone)".

Good luck!

Comment
Add comment · 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
0

Answer by tylerwongj · Mar 12, 2019 at 11:31 PM

I actually had to use sharedMaterial:

prototype.GetComponent<MeshRenderer>().sharedMaterial.SetColor("_Color",Color.red);

Comment
Add comment · 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

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

16 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 avatar image

Related Questions

GUI button texture different color when script is on Instantiated object 0 Answers

change the colour of mass placed tree leaves (or make them transparent) 1 Answer

Why is instantiated animator prefabs are not working properly? 2 Answers

How would I change the colour of a prefab relative to the size of the character? 1 Answer

Changing position of instantiated prefab from empty gameobject which is the location of the prefab when instantiating. 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