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
1
Question by Simon Foster · Dec 16, 2010 at 01:53 PM · arraycolormaterials

Changing the material in an array of materials at run time

I've been going over this for a couple of days and I'm stumped. This is hopefully a clear update on a question I asked 2 days ago.

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_BallRed. 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. I figure the best way is to have a series of materials called m_BallRed, m_BallBlue, m_BallYellow etc. When circumstances dictate I want to invoke a ChangeColor() function on the ball which currently contains the following line:

transform.Find("Plane").GetComponent().materials[0] = new Material(Shader.Find("m_BallYellow"));

Once that works I can set about customising the Function, maybe to accept a parameter to specify which color to change the ball to, but I'm getting a NullReferenceException at run time - 'UnityEngine.Material..ctor (UnityEngine.Shader shader)' for that line. So I guess I'm trying to access something that isn't there. Or I'm accessing the chlild Plane's MeshRenderer array of materials wrong. Or... or.... ? Anyone?

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

2 Replies

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

Answer by Ejlersen · Dec 16, 2010 at 02:19 PM

You could just have an array with the three materials, when wanting to change material you then use this array.

E.g.

MeshRenderer planeRenderer; Material[] mats; (You could use the inspector to assign the three materials)

Awake() { planeRenderer = GetComponentInChildren<MeshRenderer>(); }

ChangeColour(Color c) { if (c == Color.red) planeRenderer.material = mats[0]; else if (c == Color.blue) planeRenderer.material = mats[1]; else planeRenderer.material = mats[2]; }

Try not to use Find all the time, since that can be expensive. Instead cache the things you need.

  • Oh, and the code is just off the top of my head, so if there are any mistakes in the code. Sorry :)
Comment
Add comment · Show 4 · 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 Statement · Dec 16, 2010 at 03:00 PM 0
Share

Yeah, much better approach to have it cached and look them up in an array.

avatar image Simon Foster · Dec 16, 2010 at 11:46 PM 0
Share

...and that's helped me a lot! I had to tweak my code somewhat to get it to fit but I now have a good base to move forward with. Tak tak!

avatar image Simon Foster · Dec 16, 2010 at 11:47 PM 0
Share

Whoops! I got my helpers mixed up! Tak tak = thanks a lot!

avatar image Statement · Dec 17, 2010 at 12:19 AM 0
Share

Vassego! (7 more characters)

avatar image
3

Answer by Statement · Dec 16, 2010 at 02:11 PM

But I'm getting a NullReferenceException at run time - 'UnityEngine.Material..ctor (UnityEngine.Shader shader)' for that line. So I guess I'm trying to access something that isn't there.

Yeah, your Shader.Find didn't find anything. (It return null if nothing was found).

So your question is basically condensed to:

Why doesn't Shader.Find("m_BallYellow") return my expected shader?

Your material is called m_BallYellow. With your code, you are trying to find the shader that is called m_BallYellow. But there is no such shader, only such material!

So my answer?

You probably want to find the "Particles/Alpha Blended" shader, and set properties on that.

Material newMaterial = new Material(Shader.Find("Particles/Alpha Blended"));
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 Simon Foster · Dec 16, 2010 at 11:48 PM 0
Share

Thanks for getting back to me. You gave me some clear thinking here. Goodness knows what a programmer needs that for, but thanks again!

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

1 Person is following this question.

avatar image

Related Questions

Change color of all buttons listed in array 3 Answers

How can I use the same script on multiple objects without conflicting variables. 2 Answers

Brightening the colour of a material that can be any colour without shaders. 1 Answer

Change material color of array objects 1 Answer

How to change the color of a material in an array at run time 3 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