Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Mpsteve137 · Oct 27, 2017 at 04:15 PM · materialcomponentchildrencolors

How do I store the colors of children in an array?

Okay, so I'm trying to script in a little code that will make an enemy in my game flash red when I attack them. I already have figured out how to make them turn red:

foreach (Renderer r in GetComponentsInChildren ()) { r.material.color = Color.red; }

but I can't think of a way to make them return to their original colors. I was thinking I could store each children's original color in an array but I don't know a good way to do that. Please help

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
2

Answer by MacDx · Oct 27, 2017 at 06:09 PM

You already know how to get the renderers that contain the colors. You just need declare an Color array variable and store the colors there before changing them to red. Do something like this:

 //Get ahold of the renderers
 Renderer[] renderers = GetComponentsInChildren<Renderer>();
 
 //Declare a color array
 Color[] originalColors = new Color[renderers.Length];
 
 //Save the original colors into the array
 for(int i = 0 ; i<originalColors.Length ; i++)
 {
     originalColors[i] = renderers[i].maetrial.color;
 }
 //Now that the original colors are saved you can change the color to red
 
 foreach(Renderer r in renderers)
 {
     r.material.color = Color.red;
 }
 
 //EDIT: If this code is inside a coroutine then you can do
 // yield return new WaitForSeconds(0.5f);
 
 //And then when you want to turn them back to the original color you just do
 for(int i = 0; i<originalColors.Length; i++)
 {
     renderers[i].material.color = originalColors[i];
 }
 

Note that in this code I wrote, renderers and originalColors are local variables, but you could easily turn them into fields instead so you can use them elsewhere in your code, or if you are already using a coroutine for this code then you could just yield for some time and use them as they are here.

Comment
Add comment · Show 3 · 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 Mpsteve137 · Oct 28, 2017 at 01:36 AM 0
Share

Thank you so much

avatar image Mpsteve137 · Oct 29, 2017 at 11:51 PM 0
Share

I am getting an error saying that "Object reference not set to an instance of an object" at the start of the four loop that changes the colors back. Any suggestions?

avatar image 230000751 Mpsteve137 · Oct 30, 2017 at 01:13 AM 0
Share

This is either an error in your renderer list or your color list. Try printing the length of each list before running the loop to see what has fewer instances.

Even better, make your arrays temporarily public and observe them in the inspector when the error is thrown. That way you can figure it out clearly.

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

85 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 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 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 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 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

How do I access the renderer of a child from the parent gameObject? 1 Answer

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

Add script to many multiple children 1 Answer

Change material of all the children 2 Answers

How to change specific material shared among specific children. 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