• 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
Question by icerobocop · Jun 19, 2014 at 12:47 AM · spriterenderingcolor

Changing the color of all children in an empty gameobject

I have an empty gameobject with a bunch of sprites put into it to construct a monster. I want to be able to easily change the monster's color depending on some variables. Is there any easy way of changing all of the monster's color without having to change each individual sprite renderer? Something like adding a filter or renderer on top and doing a simple renderer.material.color = Color.red; code?

Comment
gregacuna

People who like this

1 Show 1
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 icerobocop · Jun 22, 2014 at 07:06 PM 0
Share

Thanks, I ended up using the foreach loop, it was not the best but it served its purpose.

2 Replies

  • Sort: 
avatar image

Answer by UnityDevelopper · Jun 19, 2014 at 01:26 AM

this code is in c# (you didn't specify which language);

 //if 3d
 foreach(Renderer variableName in GetComponentsInChildren<Renderer>()){
 r.material.color=Color.red
 }
 //if 2d with sprite 
         foreach(SpriteRenderer  variableName in GetComponents<SpriteRenderer> ()) {
             c.material.color = Color.red
         }

Hope this helps

Comment
D1sp0s3r
LoneWolfe007
theZetoranian
ow3n
xpath
yokoaiii

People who like this

6 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 ninjapretzel · Jun 19, 2014 at 01:31 AM 4
Share

I know it should be obvious for most reading this, but you need to use actual colors, and be consistant with variable naming:

 //if 3d
 foreach(Renderer r in GetComponentsInChildren<Renderer>()){
     r.material.color= Color.white;
 }
 //if 2d with sprite 
 foreach(SpriteRenderer sr in GetComponentsInChildren<SpriteRenderer>()) {
     sr.material.color = Color.red;
 }
avatar image

Answer by Owen-Reynolds · Jun 19, 2014 at 03:59 PM

No. You can use one command which individually changes each child (UD's reply.) But there's no one place you can change that will do that.

If all the sprites had the same shared material, you could change that one color. But, I'm assuming they use different head/body ... textures, which means there's no way they can share mats.

Meshes aren't grouped by gameObject, so the "parent filer" idea isn't a thing. The system might draw all heads first, then all arms.

I think, in practice, you won't want to change them all to the same color anyway. A red monster might want the hands to stay the same, the head might look good tinted (1,0,0) red, but the chest looks better at (0.8, 0.2, 0.2) red, and the legs might need a texture swap. It might be better if each monster color has a Material for each body part, instead of just a color. That will allow you to set color and texture and shader for each colored body part. You'll still have to change them individually for each child.

Comment
astracat111

People who like this

1 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 MillZzie · Oct 21, 2014 at 09:44 AM 0
Share

That's Awesome!!! Is there anyway I can do this by switching each sprite so Cat ear for dog ear and so on. So that effectively I could Completely change what my character is by just switching out the sprites. Also Developing in c#. Thanks.

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta on June 13. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

Unity changes the colours of my sprite 0 Answers

How can I change the Value of a Color through script? 1 Answer

Unity change my color variation?? 0 Answers

Why are the UI Textures darkening when Built? 1 Answer

Uniform face colour flat shading using Shader Graph 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