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 Saybejay · Apr 25, 2013 at 11:29 PM · colormaterialsarraystint

How do you change the tint of an entire material array for differing objects?

So I have multiple objects sharing one script. Part of this script is used to change the tint of the objects that have it attached. Here's the script portion I'm talking about:

 var materials : Material[];
 
 function Start ()
 {
     materials = renderer.materials;
     materials[0].color = new Color(0.5, 0.5, 0.5);
     materials[1].color = new Color(0.5, 0.5, 0.5);
     materials[2].color = new Color(0.5, 0.5, 0.5);
     materials[3].color = new Color(0.5, 0.5, 0.5);
     materials[4].color = new Color(0.5, 0.5, 0.5);
     materials[5].color = new Color(0.5, 0.5, 0.5);
     materials[6].color = new Color(0.5, 0.5, 0.5);
     materials[7].color = new Color(0.5, 0.5, 0.5);
     materials[8].color = new Color(0.5, 0.5, 0.5);
     materials[9].color = new Color(0.5, 0.5, 0.5);
     materials[10].color = new Color(0.5, 0.5, 0.5);
     materials[11].color = new Color(0.5, 0.5, 0.5);
     materials[12].color = new Color(0.5, 0.5, 0.5);
     materials[13].color = new Color(0.5, 0.5, 0.5);
     materials[14].color = new Color(0.5, 0.5, 0.5);
     materials[15].color = new Color(0.5, 0.5, 0.5);
 }
 
 function Update ()
 {
     if (owned)
     {
         materials[0].color = new Color(1, 1, 1);
         materials[1].color = new Color(1, 1, 1);
         materials[2].color = new Color(1, 1, 1);
         materials[3].color = new Color(1, 1, 1);
         materials[4].color = new Color(1, 1, 1);
         materials[5].color = new Color(1, 1, 1);
         materials[6].color = new Color(1, 1, 1);
         materials[7].color = new Color(1, 1, 1);
         materials[8].color = new Color(1, 1, 1);
         materials[9].color = new Color(1, 1, 1);
         materials[10].color = new Color(1, 1, 1);
         materials[11].color = new Color(1, 1, 1);
         materials[12].color = new Color(1, 1, 1);
         materials[13].color = new Color(1, 1, 1);
         materials[14].color = new Color(1, 1, 1);
         materials[15].color = new Color(1, 1, 1);
     }
 }

So the objects are supposed to start out dark, and when 'owned' becomes true, they become lighter. This actually works. I have no problem getting the objects to change their tint. My problem is that not all of the objects have the same amount of materials, most of them less than 15. Because of this I am getting the error, IndexOutOfRangeException: Array index is out of range. So I know why I'm getting the problem, but I was hoping there was a better way of writing my code, so it would only affect the amount of materials that are actually on each individual game object. I guess what I'm askingis that, is there a way to say something like, materials[allMaterialsOnThisObject].color = new Color(0.5, 0.5, 0.5), instead of having that huge block of code for each material in the array?

The game still works even with these errors, they just bother me. I'm sure there are other ways to circumvent this error, like having each script individualized to get the proper amount of materials for each object. I just wanted to know if anyone knew of a way to do what I was suggesting.

Thanks in advance.

Comment
Add comment · 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 Eric5h5 · Apr 25, 2013 at 11:51 PM 2
Share

Never hard-code a bunch of array entries like that; use a loop.

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by cagezero · Apr 26, 2013 at 01:35 AM

As Eric5h5 suggested, try using a loop:

 for(int x = 0; x < materials.Length; x++) {
     materials[x].color = newColor;
 }
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 Saybejay · Apr 27, 2013 at 05:21 PM 0
Share

Thank you. This was exactly what I wanted.

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

13 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

Related Questions

How to change object's material while preserving material's color? 0 Answers

Texture Brightness is darker when Loading from another Scene 0 Answers

How to set color to gameObject respective to below coding? 2 Answers

Fade In _TintColor Over Time 2 Answers

Brightening the colour of a material that can be any colour without shaders. 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