• 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 Simon Foster · Dec 13, 2010 at 12:33 PM · arraymaterialcolor

How to change the color of a material in an array at run time

I have a plane with a mesh renderer attached which has an array of two materials, m_Ball (Element 0) and m_BallHighlight (Element 1.) The tint of m_Ball is currently red. I'm trying to write a function in c# that can change the tint of Element 0 to a different tint at a defined time during run time but I don't know how to do it. I've tried typing various keywords and seeing what options come up after the dot but to no avail. Any help would be much appreciated.

Comment
oliver-jones

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 Kourosh · Dec 13, 2010 at 12:40 PM 0
Share

are you ok with JS?

2 Replies

· Add your reply
  • Sort: 
avatar image

Answer by equalsequals · Dec 13, 2010 at 03:07 PM

When dealing with a Material array, and you want to change an index of that array, you need to actually reassign the entire array for it to recognize the change.

renderer.materials = new Material[2]{new Material(Shader.Find("MyShader")),renderer.materials[1]};

So here I created a new Material[] and assigned a new Material to index 0, but used a reference to the current index 1 of our renderer.materials array.

That is very generic code, so you may need to customize it to your needs but the principle would be the same in getting it to recognize that the array has changed.

Hope that helps

==

Comment
oliver-jones

People who like this

1 Show 0 · 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

Answer by Simon Foster · Dec 13, 2010 at 04:43 PM

I'm sorry if I wasn't being clear. As a newbie you're always worried about giving too much info or not enough.

I have a ball object with script, collider, rigid body etc. attached. It also has a Plane mesh with a Mesh Renderer attached. The Mesh Renderer has a 2 element array to hold materials. Both are of shader type Particles/Alpha Blended so I don't have to worry about lighting. Element 0 is called m_Ball. It has a texture map which was originally greyscale but I've set the Tint Color to a red color - that's what I want to change when the game is played. Element 1 is called m_BallHighlight. Element 1 won't change; it's a texture map with an alpha channel which sits on top of the main texture map so I can have highlights that aren't affected by the underlying Tint in Element 0.

When the game is playing I have many different coloured balls in play. I want to change the tint of element 0 of any particular ball to one of several different colors. So I've created a public ChangeColor() function inside the Ball Script and will probably pass in one of 7 colors depending on what it's collided with.

I'm guessing that my first task is to access Element 0 of the Materials array which is on the Mesh Renderer Component attached to the Plane Mesh, which is a child of the Ball Prefab. Then I have to change it. Do I do that by entering rgb values 0-255 or 0-1.0. or do I define a series of materials in Red, Blue, Yellow etc and swap the material in element 0 for them, & will that affect all the other instances of the Ball prefab?

I hope I've made my problem more clear instead of burying it under too much detail. Thank you for the replies so far. I do appreciate them.

Comment

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Changing two different objects renderer colour 1 Answer

Array breaking when pushing floats into it via a for loop. 2 Answers

Adding a Color Change Script to a Model 1 Answer

Making Object Flash? 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