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.