Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by soydrp · Jan 25, 2016 at 11:25 AM · c#renderermaterial coloronmouseenteronmouseexit

Color transtition not rendering

Hi - I'm trying to trigger and smooth color transition when I hover on a cube, and back to the original color when exiting. You'll find the code below: it does change the color but ignores the LERP (the intermediate colors). What's more strange is that the renderer.material.color is properly updated with all lerped values(proven by the debug log), but just not rendered on screen (only until the last value). Not sure what Im missing.

 public Color colorOn;  //change in inspector
     public Color colorOff;//change in inspector
     public float duration; //secs for the transition - change in inpector
 
     float t = 1;
     bool turnOn;
     bool turnOff;
 
     private Renderer therenderer;
 
 void Start()
     {
 
         therenderer = GetComponent<Renderer>();
         therenderer.material.color = colorOff;
 
     }
 
 void OnMouseEnter()
     {
 
         turnOn= true;
         turnOff = false;
         Debug.Log("Trun ON!!)");
         turnItOn();
 
     }
 
 void OnMouseExit()
     {
         turnOn = false;
         turnOff = true;
         Debug.Log("Trun OFF!!)");
         turnItOff();
     }
 
 void turnItOn() {
         while (t > 0 && turnOn)
         {
             therenderer.material.color = Color.Lerp(colorOff, colorOn, t);
             t -= Time.deltaTime / duration;
             Debug.Log(therenderer.material.color.ToString());
         }
         t = 1;
         turnOn = false;
     }
 
     void turnItOff()
     {
         while (t > 0 && turnOff)
         {
             therenderer.material.color = Color.Lerp(colorOn, colorOff, t);
             t -= Time.deltaTime / duration;
             Debug.Log(therenderer.material.color.ToString());
         }
         t = 1;
         turnOff = false;
     }
 
 }
 

Thanks!

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 soydrp · Feb 03, 2016 at 09:15 PM

I figured it out:

 using UnityEngine;
 using System.Collections;
 
 public class ChangeBarWhenHover : MonoBehaviour {
 
 
     Color colorOn = Color.gray;
     Color colorOff = Color.white;
     float duration = 0.6f;
 
     float t;
     bool turnOn;
     bool turnOff;
 
     private Renderer renderer;
 
 void Start()
     {
         GetComponent<Renderer>().material.color = colorOff;
 
         renderer = GetComponent<Renderer>();
         //normal = Shader.Find("Standard");
         //hover = Shader.Find("FX/Gem");
     }
 
 void OnMouseEnter()
     {
 
         turnOn= true;
         turnOff = false;
         t = duration;
         StartCoroutine (turnItOn());
 
         //        GetComponent<Renderer>().material.color = Color.gray;
 
     }
 
 void OnMouseExit()
     {
         turnOn = false;
         turnOff = true;
         t = duration;
         StartCoroutine (turnItOff());
         //GetComponent<Renderer>().material.color = Color.white;
     }
 
 IEnumerator turnItOn() {
         while (t > 0 && turnOn)
         {
             //print(t + " Fade in" + " Fade out");
             renderer.material.color = Color.Lerp(colorOn, colorOff, t);
             t -= Time.deltaTime / duration;
             //Debug.Log("Color oning: " + renderer.material.color);
             yield return 0;
         }
 
         turnOn = false;
     }
 
 IEnumerator turnItOff()
     {
         while (t > 0 && turnOff)
         {
             //print(t + " Fade in" + " Fade out");
             renderer.material.color = Color.Lerp(colorOff, colorOn, t);
             t -= Time.deltaTime / duration;
             //Debug.Log("Color offing: " + renderer.material.color);
             yield return 0;
         }
 
         turnOff = false;
     }
 
 }
Comment
Add comment · 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.

Follow this Question

Answers Answers and Comments

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

Related Questions

OnMouseEnter OnMouseExit, and Event Triggers Not Working? 1 Answer

OnMouseEnter and OnMouseExit for multiple materials 0 Answers

How to activate UI element on mouse over? 1 Answer

OnMouseEnter and OnMouseExit collide (I guess) and make an animation loop for no reason. 0 Answers

Sprite Alpha Color Won't Change Via 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