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.

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.

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.

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