- Home /
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.
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?
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.
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.
Tried that! $$anonymous$$aterial always turns black on mouse exit.
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.
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;
}
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.
Just as I thought. This still causes the object to turn black upon $$anonymous$$ouseExit.
Any other ideas?
Your answer
Follow this Question
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