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 paco morales · Feb 12, 2013 at 09:05 PM · colorrenderervanish

How to change the color using "renderer.material.color"?

Hello Everyone,

I'm trying to make some arrows in order to show the parts of my 3d object. When I press the button to enable and show my arrows they become visible in white.

But I want to change the color to red when my mouse is over of certain button.

Now I can enabled and vanish my arrows and change the color to red from white, when the mouse cursor is over the button or Rect area assigned to change the color.

But when the cursor is outside of the Rect assigned to change the color. The color white is not back again.

Any advice is more than welcome!

Please take a look to this little short video.

Link video

Please take a look to my Script.

     private var alpha: float = 1; // alpha starts at 1
     private var vanish = false;
             var fadeSpeed : float = 7.5;
       
   
     function Update(){
  
     if(vanish){
     renderer.material.color.a = Mathf.Lerp(renderer.material.color.a, 1, Time.deltaTime * fadeSpeed);
     } else {
     renderer.material.color.a = Mathf.Lerp(renderer.material.color.a, 0, Time.deltaTime * fadeSpeed);
     }
     if(guiNew .alphaGo == 1){  
     t01();
     guiNew . alphaGo = 0;
     }
     if(guiNew .alphaGo == 2){
     t02();
     guiNew . alphaGo = 0;
     }
      if(Input.mousePosition.x>890&&Input.mousePosition.y>385&&Input.mousePosition.x<890+60&&Input.mousePosition.y<385+42){
     renderer.material.color = Color.red;
     }
     }
     
 /////////////////////////////////////////
     
     function t01(){
     vanish = true;
     }
     
     function t02(){
     vanish = 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
1
Best Answer

Answer by PAEvenson · Feb 12, 2013 at 09:19 PM

the easiest way would be add an else to your mousePosition if statement

 if(Input.mousePosition.x>890&&Input.mousePosition.y>385&&Input.mousePosition.x<890+60&&Input.mousePosition.y<385+42){
     renderer.material.color = Color.red;
 }
 else{
     renderer.material.color = Color.white;
 }
Comment
Add comment · Show 5 · 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 paco morales · Feb 12, 2013 at 09:27 PM 0
Share

Hello PAEvenson,

I did but my Vanish function is not working any more. It change the color to red from white, but if I want to vanish the arrows I can't.

avatar image PAEvenson · Feb 12, 2013 at 09:31 PM 0
Share

ha ok i see the problem:

 if(Input.mousePosition.x>890&&Input.mousePosition.y>385&&Input.mousePosition.x<890+60&&Input.mousePosition.y<385+42){
     renderer.material.color = Color.red;
 }
 else{
     Color newColor = Color.white;
     newColor.a = renderer.material.color.a;
     renderer.material.color = newColor;
 }
avatar image paco morales · Feb 12, 2013 at 09:47 PM 0
Share

Thanks for your help, now is showing only one error, $$anonymous$$aybe I $$anonymous$$issed Something!

Please take a look :)

     private var alpha: float = 1; // alpha starts at 1
     private var vanish = false;
             var fadeSpeed : float = 7.5;
       
   
     function Update(){
  
     if(vanish){
     renderer.material.color.a = $$anonymous$$athf.Lerp(renderer.material.color.a, 1, Time.deltaTime * fadeSpeed);
     } else {
     renderer.material.color.a = $$anonymous$$athf.Lerp(renderer.material.color.a, 0, Time.deltaTime * fadeSpeed);
     }
     if(guiNew .alphaGo == 1){  
     t01();
     guiNew . alphaGo = 0;
     }
     if(guiNew .alphaGo == 2){
     t02();
     guiNew . alphaGo = 0;
     }
     if(Input.mousePosition.x>890&&Input.mousePosition.y>385&&Input.mousePosition.x<890+60&&Input.mousePosition.y<385+42){
     renderer.material.color = Color.red;
     }else{
     Color newColor = Color.white;     // in this line is showing error Assets/Scripts/arrows.js(24,10): UCE0001: ';' expected. Insert a semicolon at the end.
     newColor.a = renderer.material.color.a;
     renderer.material.color = newColor;
     }
     }
     
 /////////////////////////////////////////
     
     function t01(){
     vanish = true;
     }
     
     function t02(){
     vanish = false;
     }
     
avatar image PAEvenson · Feb 12, 2013 at 09:48 PM 0
Share

oops sorry, you need it in JS:

 var newColor :Color = Color.white;
avatar image paco morales · Feb 12, 2013 at 10:12 PM 0
Share

PAEvenson thanks, now is working perfectly!

Thanks Again.

avatar image
0

Answer by phfatmonkey · Feb 12, 2013 at 09:53 PM

Looks like you never set the color back to white once your mouse leaves the rect. Also, you are constantly setting the color every frame while the mouse is inside the rect.

Maybe try putting a bool to test whether it's on or not. Then, add a function that will change the color based on the parameter you pass to it.

Try this, at the top of your class, add a private variable:

 var selected = false;

then in your update function, change your mouse over check to this:

     if(Input.mousePosition.x>890&&Input.mousePosition.y>385&&Input.mousePosition.x<890+60&&Input.mousePosition.y<385+42)
     {
         if (!selected)
         {
            selected = true;
            ToggleArrow(selected);
         }
     
     }
 else
 {
     if (selected)
     {
         selected = false;
         ToggleArrow(selected);
     }
 
 }

Then add a new method that will change the color based on whatever you pass to it:

 function ToggleArrow(var selected)
 {
     if (selected)
     {
         renderer.material.color = Color.red;
     }
     else
     {
         renderer.material.color = Color.white;
     }
 
 }

FYI, there are a few GUI packages that might make it easier to handle the UI. EZGUI is one, and we've also tried UIToolKit from Prime31. It's free and open-source--not the ideal solution but I thought it worked well.

P.S. I apologize if my JS syntax is incorrect, I primarily use C#.

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 paco morales · Feb 12, 2013 at 10:13 PM 0
Share

Ryan, thanks for your help and I will take your advice :)

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

11 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

Related Questions

Changing two different objects renderer colour 1 Answer

Vanish one object with renderer.material? 1 Answer

A node in a childnode? 1 Answer

Massive FPS hit when changing colors on the fly 3 Answers

Changing material color creates a memory leak 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