x


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.

more ▼

asked Mar 31, 2012 at 12:03 PM

thundax gravatar image

thundax
3 10 9 12

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;} OnMouseOver{renderer.material.color = green; } OnMouseExit{renderer.material.color = white; } Is it?

Mar 31, 2012 at 12:26 PM fafase

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.

Mar 31, 2012 at 01:44 PM thundax
(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

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.

more ▼

answered Mar 31, 2012 at 01:19 PM

garner gravatar image

garner
120 1 2 2

Tried that! Material always turns black on mouse exit.

Mar 31, 2012 at 01:45 PM thundax
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Mar 31, 2012 at 01:19 PM

garner gravatar image

garner
120 1 2 2

(comments are locked)
10|3000 characters needed characters left

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;
 }
more ▼

answered Mar 31, 2012 at 01:02 PM

Bunny83 gravatar image

Bunny83
85.9k 31 169 438

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 onMouseEnter 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 naming conventions and see if that helps. Otherwise I've basically already done what you posted here in the past.

Thanks for the help though.

Mar 31, 2012 at 01:54 PM thundax

Just as I thought. This still causes the object to turn black upon MouseExit.

Any other ideas?

Mar 31, 2012 at 02:02 PM thundax
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1480
x1451
x958
x514
x70

asked: Mar 31, 2012 at 12:03 PM

Seen: 2303 times

Last Updated: Mar 31, 2012 at 02:02 PM