• 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 ProjectCryken · Nov 01, 2013 at 12:19 PM · colorscriptable object

When a single renderer.color is changed in a group of objects, change the remaining objects.

So for example if I have 5 cubes as children of a single empty object. How would I go about making it so that if the renderer.color of one of them cubes is changed, the other 9 change as well?

Comment
Add comment · Show 8
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 mattssonon · Nov 01, 2013 at 12:38 PM 0
Share

I'd suggest looking at the answer to this question: http://answers.unity3d.com/questions/25392/public-variable-listener-for-dispatch-in-composed.html

avatar image roojerry · Nov 01, 2013 at 01:01 PM 0
Share

Why not just give all the cubes that need to change color a shared material. That way, changing the material color changes all of the cubes with that material.

avatar image ProjectCryken · Nov 01, 2013 at 03:09 PM 0
Share

The documentation doesn't make it clear to me how shared materials work and how to use them.

avatar image roojerry · Nov 01, 2013 at 03:17 PM 0
Share

ins$$anonymous$$d of setting renderer.material = your$$anonymous$$aterial; (which creates a new instance of the your$$anonymous$$aterial for each object its on)

you would set renderer.shared$$anonymous$$aterial = your$$anonymous$$aterial;

Now each object that you set to that shared material will reflect any changes you make to that material, i.e. the color you want to change.

avatar image robertbu · Nov 01, 2013 at 04:12 PM 0
Share

To expand on the shared$$anonymous$$aterial mentioned by @brianruggieri. When changing the shared$$anonymous$$aterial, you are modifying the 'base' material permanently. Create a few object and add the same material to each. Then add the following script to one or more of the objects:

 #pragma strict
 
 function Start() {
     renderer.shared$$anonymous$$aterial.color = Color.red;
 }
 
 function On$$anonymous$$ouseDown() {
     renderer.shared$$anonymous$$aterial.color = Color.green;
 }
Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by naglers · Nov 01, 2013 at 04:17 PM

You can either use the shared material option that Brianruggieri suggested

 renderer.material = yourMaterial; //to
 renderer.sharedMaterial = yourMaterial;
 
 //or you can have a script on each box that when you change the color of one it calls
 
 foreach(Renderer r in transform.parent.GetComponentsInChildren())
 {
     foreach(Material M in r.materials)
     {
         M.color = new Color(1,1,1,1);
     }
 }

this way if you have multiple groups of boxes it won't affect them but if you don't I recommend the SharedMaterial way as it is much more efficient

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 ProjectCryken · Nov 01, 2013 at 05:33 PM 0
Share

The problem I have is most of the level uses the same material, so I don't want to change every single object's color. Preferably just the ones that are children of any given object, with one script per group.

Here is the actual use. The game is based on swapping colors from various objects to the player and back. Swap with left click. Notice the wall behind the door in front of the player is constructed of three cubes, I want that wall to share the same color at all times to appear as one object.

Game

avatar image naglers · Nov 01, 2013 at 09:14 PM 0
Share

the above code is for that, I figured you might not want to use the shared material thing since you probably use multiple instances

avatar image
0

Answer by robertbu · Nov 01, 2013 at 05:51 PM

Attach this script to a parent. The parent and all the children will change color when you click on a parent.

 #pragma strict
 
 function OnMouseDown() {
     for (var rend : Renderer in gameObject.GetComponentsInChildren(Renderer)) {
         rend.material.color = Color.cyan;
     }
 }
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 ProjectCryken · Nov 01, 2013 at 06:48 PM 0
Share

The parent is just an empty game object so they can't really click on it, It has to be when they click on a child they all change.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

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

Related Questions

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

Changing two different objects renderer colour 1 Answer

Why are all colors inverted in unity editor and build? Please help,Why is unity displaying inverted colors? Please help need a fix immediately. 2 Answers

How to edit this shader to use diffuse lighting (2D) 0 Answers

How can I add a color, smoothness and metallic property to this shader? 1 Answer


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