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 Texian · Jun 22, 2015 at 09:10 PM · c#materialstexturesvisibilityswap

Cycle through object materials on mouse click in C#

I have an object in my scene - a projection screen - that I need to cycle through different "slides" on a mouse click. I've tried working with a bunch of different examples I've come across, but it seems like the API update with Unity 5 (combined with my lack of experience) is throwing me for a loop. The chunk of code I've come up with seems like it should work, but when I run the game, I get bupkis other than the default material.

 using UnityEngine;
 using System.Collections;
 
 public class SwapTextures : MonoBehaviour
 {
     public Material[] myMaterials;
     int arrayPos = 0;
     
     void updateMaterials ()
     {
         if (Input.GetMouseButtonDown (0)) {
             arrayPos++;
             arrayPos %= myMaterials.Length;
             GetComponent<Renderer> ().material = myMaterials [arrayPos];
         }
     }
     
     void Update ()
     {
         updateMaterials ();
     }
 }
 
Comment
Add comment · Show 5
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 Cherno · Jun 22, 2015 at 09:18 PM 0
Share

Seems fine to me as well. What exactly happens when you click?

avatar image Texian · Jun 22, 2015 at 09:24 PM 0
Share

Absolutely nothin'. Here's what's in the Inspector: alt text

screen-shot-2015-06-22-at-22408-pm.jpg (107.9 kB)
avatar image Cherno · Jun 22, 2015 at 09:55 PM 0
Share

I suspect that the problem lies with the $$anonymous$$eshRenderer, specifically that it has multiple materials, but you use Renderer.material in your code. Use Renderer.materials ins$$anonymous$$d:

 void update$$anonymous$$aterials ()
 {
     if (Input.Get$$anonymous$$ouseButtonDown (0)) {

         Renderer renderer = GetComponent<Renderer>();
         for(int i = renderer.materials.Length - 1; i > 0; i--) {
             renderer.materials[i] = my$$anonymous$$aterials [arrayPos];
         }
     }
     
 }

We are using a reverse loop here just to be sure. A foreach loop won't work here because we try to change the elements in the array from inside the loop, and the reverse loop allows us to avoid any indexing problems (although this is more relevant when looping through a list and deleting elements)

avatar image Key_Less · Jun 22, 2015 at 11:32 PM 0
Share

Your Swap Textures script works fine. What type of shader are your Brocade x materials using? Does the shader type require a texture (such as Unlit/Texture), if so, do the materials have textures assigned to them?

avatar image Texian · Jun 23, 2015 at 12:09 AM 0
Share

Still nothin'. I copied the code exactly and replaced the old code with it and it still didn't do anything.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Key_Less · Jun 23, 2015 at 01:30 AM

I see you're using the same material multiple times within your 'MeshRenderer' component; more than one material should only be used here if your model requires multiple materials for rendering. There is no point iterating through the materials list if all materials are the same. Are you only changing one material, or will you eventually be using multiple materials for the 'Mesh1' model?

If you are only changing a single material, I would use your original code which works perfectly fine. However I would suggest using 'sharedMaterial' instead of 'material'.

If you're planning on changing all the materials, remember that all arrays in Unity return a copy, meaning that if you'd like to change the materials array you'll need to get a copy of the materials array, edit the copy, and then set the materials back to the object.

Here are a couple examples of what you could do:

1) If you're changing multiple materials:

 using UnityEngine;
 
 public class SwapTextures : MonoBehaviour 
 {
     public Material[] MyMaterials;
     
     private int arrayPos;
     private Material[] modelMaterials;
 
     void Start()
     {
         modelMaterials = GetComponent<MeshRenderer>().sharedMaterials;
     }
 
     private void UpdateMaterials()
     {
         if(Input.GetMouseButtonDown(0) && MyMaterials.Length > 0)
         {
             arrayPos++;
             arrayPos %= MyMaterials.Length;
 
             // If changing multiple materials...
             for(var i = 0; i < modelMaterials.Length; i++)
             {
                 modelMaterials[i] = MyMaterials[arrayPos];
             }
             GetComponent<MeshRenderer>().sharedMaterials = modelMaterials;
         }
     }
 
     void Update()
     {
         UpdateMaterials();
     }
 }
 

2) If you're changing a single material:

 using UnityEngine;
 
 public class SwapTextures : MonoBehaviour 
 {
     public Material[] MyMaterials;
     
     private int arrayPos;
 
     private void UpdateMaterials()
     {
         if(Input.GetMouseButtonDown(0) && MyMaterials.Length > 0)
         {
             arrayPos++;
             arrayPos %= MyMaterials.Length;
             GetComponent<MeshRenderer>().sharedMaterial = MyMaterials[arrayPos];
         }
     }
 
     void Update()
     {
         UpdateMaterials();
     }
 }
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 Texian · Jun 23, 2015 at 05:13 PM 0
Share

That did it! Had to tweak one or two things (and then remember to turn the script back on after turning it off for debugging :P ) and it works!

Thank you, $$anonymous$$ey_Less, and everyone else for chi$$anonymous$$g in to help out. This is all immensely appreciated. :)

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Changing HDRP Material's Texture At Runtime Not Working? 2 Answers

Texture swapped out works in editor but not in build 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Blend 2 linerender colors at point of interception 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