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 DaveA · Jun 10, 2010 at 11:32 PM · shaderprefabcolor

Setting color on prefab with shader not working?

I have this prefab which contains a Plane mesh, which uses the Transparent/Diffuse shader. The texture is a gray PNG with alpha, and I want to use the shader's MainColor to colorize that PNG. In Editor, this is all well and good.

I have several instances of this prefab, and I need to change the colors of the various instances dynamically. So I get the prefab instances by name and set the color Code:

p = gameObject.Find("Plane"); 
if (!p) 
   Debug.LogError("spot plane not found"); 
p.renderer.material.SetColor ("_Color", scolor); 

The odd thing is, it sets ONE of the instances, not all of them, and it's not always the same instance. I can replace 'scolor' with, say, Color.red, and then one of the instance will colorize. All others stay at the default color.

I would have thought that if it was altering the shader at a 'material' level, then all instances would have the new color.

What's the trick to this?

Should say: 2.6.1 Windows, decent machine

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Eric5h5 · Jun 10, 2010 at 11:43 PM

GameObject.Find finds one single object. If you have more than one with the same name, which one it finds first has no guaranteed order. If you change renderer.material on an object, Unity creates a new unique scene material for you which is then used for that object. To change the material for all objects using that material, use renderer.sharedMaterial.

Comment
Add comment · Show 2 · 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 DaveA · Jun 11, 2010 at 01:03 AM 0
Share

I thought gameObject.Find() (note the lowercase g) would search within that gameObject. Is there a way to find game objects in children (without writing my own iterator, and still, where is the 'getChild' method?)

avatar image DaveA · Jun 11, 2010 at 01:13 AM 0
Share

Well I don't know about finding objects like that, but I replaced p = gameObject.Find("Plane"); with p = gameObject.Find(gameObject.name+"/Plane"); and now it works. Thanks for the tip.

avatar image
0

Answer by Mike 3 · Jun 25, 2010 at 01:33 AM

You can use renderer.sharedMaterial instead of renderer.material if you want to manipulate them all at the same time

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

No one has followed this question yet.

Related Questions

Material doesn't have a color property '_Color' 4 Answers

Problem getting prefab color 0 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Show and Hide a prefab or GameObject 10 Answers

cloned game object wont call script right... 2 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