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 sam111 · Oct 02, 2013 at 05:53 PM · c#materialsmaterial colorclass object

how to change object material with mouse click ?

hello,

I want to change the object material when click on object,i make this script in c# below but it change between two materials only and i want to change among three material as the third are not work,so what is the wrong with this

 using UnityEngine;
 using System.Collections;
 
 public class oo : MonoBehaviour {
 
 // put the first material here.
 public Material logo;
 // put the second material here.
 public Material material2;
 // put the third material here.
 public Material material3;
  
 bool FirstMaterial = true;
 bool SecondndMaterial = false;
 bool SecondndMaterial3 = false;
 void Start () 
 {
     renderer.material = logo;
 }
  
 void OnMouseDown()
 {
     if (FirstMaterial)
     {
         renderer.material = material2;
         SecondndMaterial = true;
         FirstMaterial = false;
         SecondndMaterial3 = false;
     }
  
     else if(SecondndMaterial)
     {
         renderer.material = logo;
         FirstMaterial = true;
         SecondndMaterial = false;
         SecondndMaterial3 = false;
     }
         else if(SecondndMaterial3)
     {
         renderer.material =material3 ;
         SecondndMaterial3 = true;
         FirstMaterial = false;
         SecondndMaterial = false;
         
 }
 }
 }
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
2
Best Answer

Answer by mattssonon · Oct 02, 2013 at 05:59 PM

You never set SecondndMaterial3 to true. Change the second if conditional to this:

 else if(SecondndMaterial)
 {
     renderer.material = logo;
     FirstMaterial = false;
     SecondndMaterial = false;
    SecondndMaterial3 = true;
 }
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 sam111 · Oct 02, 2013 at 07:19 PM 1
Share

thanks all

avatar image
5

Answer by clunk47 · Oct 02, 2013 at 05:59 PM

I'd create a simple public array of materials, set the size and materials in the inspector. Then you'd want to use an int for the index, then simply cycle through on each click.

 using UnityEngine;
 using System.Collections;
 
 public class Example : MonoBehaviour 
 {
     public Material[] materials;
     int index = 0;
     
     void Start()
     {
         renderer.material = materials[index];    
     }
     
     void OnMouseDown()
     {
         index++;
         if(index >= materials.Length)
             index = 0;
         renderer.material = materials[index];
     }
 }
 
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 pontiac4545 · Aug 03, 2021 at 04:42 PM 0
Share

Thank you! This worked for me, but I had to make sure a collider was on the affected objects. And for the newer Unity versions ($$anonymous$$e 2021), the wording is: GetComponent(less-than)Renderer(greater-than).material = materials[index];

*this forum page doesn't allow those less/greater-than symbols, its weird

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

17 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

Related Questions

Changing the color of a material during runtime (works in editor, not build) 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How do I attach a Created Material A Texture, NormalMap and OcclussionMap? 0 Answers

Can I save class instances in a prefab-like way? 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