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 Green123 · Apr 27, 2014 at 11:40 PM · materialchange material

Changing material of an object

Hi. I looked for a solution to this but couldn't find anything. What i want to do is change the material of an object every few seconds. Here is my code:

     private float countdown;
     public float originalCountdown;
     public Material white, black;
     
     void Start () {
         countdown = originalCountdown;
     }
     
     void Update() {
         countdown -= Time.deltaTime;
         if (countdown <= 0) {
             Change ();
         }        
     }
 
     void Change() {
         countdown = originalCountdown;
         if (renderer.material = black) {
             Debug.Log ("White");
             renderer.material = white;
         } else if (renderer.material = white) {
             Debug.Log ("Black");
             renderer.material = black;
         }
     }
 }

What is happening now is that the material changes from black to white and i receive the message "White". Then the material stays white and i keep receiving the message "White" every time that Change() is called.

I really can't understand what is bad with the code. Also i am not using == because it only works with =. Not sure why.

Pretty new to all this anyway.

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

1 Reply

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

Answer by robertbu · Apr 27, 2014 at 11:42 PM

For comparison operations, you need to use double equals ('=='). In addition, since you are switching, you don't need the else:

  void Change() {
        countdown = originalCountdown;
        if (renderer.material == black) {
          Debug.Log ("White");
          renderer.material = white;
        } 
     else {
          Debug.Log ("Black");
          renderer.material = black;
        }
     

}

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 Green123 · Apr 28, 2014 at 12:36 AM 0
Share

Now i receive the message "Black" and it never changes from black to white.

avatar image Green123 · Apr 28, 2014 at 12:41 AM 0
Share

It only changes when i use =, not ==.

avatar image robertbu · Apr 28, 2014 at 12:54 AM 0
Share

Single '=' is definitely not the right way to solve your issue. I'm guessing that something is changing the material instance which means the material, though black, will not evaluate to the material you assigned. You can use a boolean ins$$anonymous$$d. At the top of the file:

 private bool isBlack = false;  // Set for original color.  

Then the code changes to:

 void Change() {
        countdown = originalCountdown;
        if (isBlack) {
          Debug.Log ("White");
          renderer.material = white;
        } 
     else {
          Debug.Log ("Black");
          renderer.material = black;
        }
     isBlack = !isBlack;
 }
avatar image Green123 · Apr 28, 2014 at 01:42 AM 0
Share

Yeah it seems that works perfectly. Thanks a lot.

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

20 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 avatar image avatar image avatar image

Related Questions

Material doesn't have a color property '_Color' 4 Answers

Change material with UI button ? 0 Answers

memory consumption when changing materials [iOS] 2 Answers

Changing two different objects renderer colour 1 Answer

how to switch material with script? 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