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
1
Question by thundax · Mar 31, 2012 at 12:03 PM · materialmousecolorrendereronmouseover

Need material.color to revert.

I have a selection script here in C# that causes objects to turn Color.green when the mouse is over.

The script previously did Color.white upon mouse exit. However the team and I are now using the color properties of the materials to get more variety in our planets, and for other effects. So, we need the color of the object with the script attached to return to the original color of that object and not any other which the mouse may have been over.

Here's my code thus far. This is a per object script that we only place on select-able or camera focus-able objects.

 using UnityEngine;
 using System.Collections;
 
 public class RTSControlSelect : MonoBehaviour {
     
     public static GameObject objectName;
     public static GameObject targetName;
     public static GameObject qFocus;
     Color original;
     Color green;
     //public GameObject cursorLockTarget; 
     //bool cursorLock;
     //bool renderOriginal;
     
     void start()
     {    
         green = Color.green;
     }
     
     void Update()
     {
         //cursorLockTarget = GameObject.Find("Camera");
         //RTSCameraControl rtsCameraControl = cursorLockTarget.GetComponent<RTSCameraControl>();
         //cursorLock = rtsCameraControl.cLock;
         
         //if (cursorLock == true)
         //{ 
         //    renderOriginal = true; 
         //}
         
         //if (cursorLock == false)
         //{ 
         //    renderOriginal = false; 
         //}
         
         if (gameObject.renderer.material.color != green)
         {
             original = gameObject.renderer.material.GetColor("_Color");
         }
     }
         
     void OnMouseOver()
     {
         //if (renderOriginal == false)
         //{ 
             renderer.material.color = Color.green;          
         //}
         
         //if (renderOriginal == true)
         //{
         //    renderer.material.color = original;                
         //} 
                 
         if (Input.GetMouseButtonUp(0))
         {
             objectName = (gameObject);
         }
         
         if (Input.GetMouseButtonDown(1))
         {
             targetName = (gameObject);
         }
         
         if (Input.GetMouseButtonUp(2))
         {
             qFocus = (gameObject);
         }
     }
     
     void OnMouseExit()
     {
         renderer.material.color = original;
         
         qFocus = (null);
     }
 }

The sections with the cursorLock element are for keeping the object at the center of screen and camera focus from turning green when the player rotates the camera.

Comment
Add comment · Show 2
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 fafase · Mar 31, 2012 at 12:26 PM 0
Share

I think I don't get it, you have an object that is let's say white and it turns green when you hover over, and you want it back to white when the mouse is not over anymore, is that so? Just use function Start{renderer.material.color = white;} On$$anonymous$$ouseOver{renderer.material.color = green; } On$$anonymous$$ouseExit{renderer.material.color = white; } Is it?

avatar image thundax · Mar 31, 2012 at 01:44 PM 0
Share

Sorta. I have an object that has a custom material color setting. Not white! So I can't tell it renderer.material.color = Color.white because that wouldn't return it to it's original state before mouse over upon mouse exit.

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by garner · Mar 31, 2012 at 01:19 PM

In the start function assign the 'original' variables color not in the update, it only needs to be assigned once at start then onmouseexit will work.

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 thundax · Mar 31, 2012 at 01:45 PM 0
Share

Tried that! $$anonymous$$aterial always turns black on mouse exit.

avatar image
0

Answer by garner · Mar 31, 2012 at 01:19 PM

In the start function assign the 'original' variables color not in the update, it only needs to be assigned once at start then onmouseexit will work.

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

Answer by Bunny83 · Mar 31, 2012 at 01:02 PM

Well there are a lot things that looks a bit strange.

  • First of all your object has an individual color set in the inspector and you want to store this "original" color to be able to revert to this color, so why don't you save it in Start() once?

  • There's also a OnMouseEnter callback which get called once when the mouse is over the object.

  • I'm not sure for what purpose the variables objectName, targetName and qFocus are used but the variable names aren't well choosen. If a variable contains the word "name" everybody would expect a string in the first place

I would do something like that:

 public static GameObject selectedObject;
 public static GameObject targetObject;
 public static GameObject focusedObject;
 
 private Color originalColor;
 public Color selectedColor = Color.green;
 
 void start()
 {  
     originalColor = renderer.material.color;
 }

 void OnMouseEnter()
 {
     renderer.material.color = selectedColor;
 }

 void OnMouseOver()
 {
     if (Input.GetMouseButtonUp(0))
     {
         selectedObject = gameObject;
     }
     if (Input.GetMouseButtonDown(1))
     {
         targetObject = gameObject;
     }
     if (Input.GetMouseButtonUp(2))
     {
         focusedObject = gameObject;
     }
 }

 void OnMouseExit()
 {
     renderer.material.color = originalColor;
     focusedObject = null;
 }
Comment
Add comment · Show 2 · 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 thundax · Mar 31, 2012 at 01:54 PM 0
Share

Gotcha!

Also I tried putting "originalColor = renderer.material.color;" in "void Start". The material always turns black on mouse exit. I also can't use on$$anonymous$$ouseEnter because the selection stuff doesn't work right with that because it is only called once.

The "objectName", "targetName", and "qFocus" are to pass to the smoothfollow, orbit, and camera control script (all one script). I use a switchable parenting or position based smooth follow and focus.

I'll try your na$$anonymous$$g conventions and see if that helps. Otherwise I've basically already done what you posted here in the past.

Thanks for the help though.

avatar image thundax · Mar 31, 2012 at 02:02 PM 0
Share

Just as I thought. This still causes the object to turn black upon $$anonymous$$ouseExit.

Any other ideas?

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Changing two different objects renderer colour 1 Answer

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

Fade out a material that has a texture 1 Answer

Odd behavior with changing renderer.material.color 0 Answers

How would I change the color of a texture within an object with multiple textures? 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