- Home /
Question by
cgravat1984 · Oct 19, 2015 at 11:44 PM ·
materialonmousedownhighlightonmouseenter
Need help with my script - Rollover change material and click change material
I have a script here that I can't seem to make it do both things that I want it to do. Right now it's either/or. I would like for when the mouse is over an object, it will change material, then when it is off of it it goes back to original. Then when you click on the object it changes material until clicked in open space or on another object. Right now it's one or the other. Can someone help?!
using UnityEngine;
using System.Collections;
public class Change_Material : MonoBehaviour {
// put the first material here.
public Material Material1;
// put the second material here.
public Material Material2;
bool FirstMaterial = true;
bool SecondMaterial = false;
void Start ()
{
GetComponent<Renderer>().material = Material1;
}
void OnMouseEnter()
{
if (FirstMaterial) {
GetComponent<Renderer> ().material = Material2;
SecondMaterial = true;
FirstMaterial = false;
}
}
void OnMouseExit()
{
if (SecondMaterial) {
GetComponent<Renderer> ().material = Material1;
SecondMaterial = false;
FirstMaterial = true;
}
}
void OnMouseDown()
{
if (FirstMaterial) {
GetComponent<Renderer> ().material = Material2;
SecondMaterial = true;
FirstMaterial = false;
}
}
void OnMouseUp()
{
if (FirstMaterial) {
GetComponent<Renderer> ().material = Material2;
FirstMaterial = false;
SecondMaterial = true;
}
}
void LateUpdate ()
{
if (Input.GetMouseButtonDown (0))
if (SecondMaterial) {
GetComponent<Renderer>().material = Material1;
SecondMaterial = false;
FirstMaterial = true;
}
}
}
Comment
Your answer
Follow this Question
Related Questions
How to count numbers on onmousedown? 1 Answer
OnMouseDown 2 Answers
Enter a specific word script 0 Answers
Changing the Mouse's Range for OnMouse events 0 Answers
highlight object color 3 Answers