Hello
I am currently in the process of making a main menu.
However one of the problems I’m having is that I cant find a way to change the renderer colour of two objects when the script that changes the renderer colour of the object is only on one of the objects. What I’m actually trying to do is change the colour of the text but also change the colour of the box that is behind it so that when the text is unselected the text is white and the box is black but when the text is selected the text is black and the box is white. I am using the renderer.material.color = Color.white; code method and using a simple OnMouseEnter/Exit method to detect the Mouse hovering over it.
Here Is the code of one of the buttons
#pragma strict
function OnMouseEnter ()
{
renderer.material.color = Color.black;
}
function OnMouseExit ()
{
renderer.material.color = Color.white;
}
function OnMouseDown () {
Application.LoadLevel ("Initial Scene");
}
Hey Dok101,
Here is an old script I made for a project once, please add this to your assets in Unity and in a scene make a TextMesh.
take that textMesh and add it into this script through inspector panel.
You may also need to create upto 3 extra materials to show you whats happening during runtime, but i dont think it will throw a wobbly if you dont.
Code…
using UnityEngine;
using System.Collections;
/// <summary>
/// GUI hover controls.
/// Forces an audio source to the instance
/// plays an audio object on mouse up
/// changes colour on mouse over and exit
/// loads or plays based on textmesh being clicked on the screen
///
/// IMPORTANT!!!
/// YOU MUST MAKE A TEXT MESH AND THEN APPLY THAT TO THE SLOT IN YOUR SCRIPT FOUND THROUGH INPECTOR.
/// IT IS ALSO ADIVASBLE TO MAKE THREE MATERIALS(DIFFUSE FINE) AND CHANGE EACH COLOR.
/// THE FADED OT MATERIAL JUST NEEDS TO BE SEE THOUGH OR HAVE NO TEXTURE OR SOMETHING, SEE UNITY MOBILE SHADERS->TRANSPARENT IN MATERIAL TYPE DROP DOWN
/// </summary>
[RequireComponent(typeof(AudioSource))]
public class GUIHoverControls : MonoBehaviour
{
public int levelToLoad;
public AudioClip narrative;
public AudioClip beep;
public bool isQuitButton = false;
public float wait = 5.0f;
public Material mat1;
public Material mat2;
public Material mat3;
public Material fadedOutMat;
public TextMesh txtMesh;
// Use this for initialization
private bool isClicked = false;
/// <summary>
/// Raises the mouse over event.
/// </summary>
void OnMouseOver()
{
/// change the material when hovering over
ChangeColor(mat2);
/// if leftmouse button cllicked
if(Input.GetMouseButtonUp(0))
{
/// change mat to signify click occurence
ChangeColor(mat3);
/// if this is a wuit button
if(isQuitButton)
{
/// audio.clip = clickBeep;
audio.PlayOneShot(beep);
/// exit game
Application.Quit();
}
else /// this is a button to load a level
{
Debug.Log ("loading level in 5 secs");
/// specifics hack to check if button is start game to allow button audio to yield before starting the level
if(txtMesh.text == "Another Button")
{
/// GUI.Label (new Rect(Screen.width/2,Screen.height /3,100,40), "Click to for Intro");
isClicked = true;
Debug.Log (isClicked);
StartCoroutine(StartIntroTalk());
}
else if(txtMesh.text == "Play gamE")
{
isClicked = true;
Debug.Log (isClicked);
StartCoroutine(LoadALevel());
}
else /// we checked and it wasnt the start button
{
isClicked = false;
Debug.Log (isClicked);
/// StartCoroutine(LoadChosenLevel());
}
}
}
}
/// <summary>
/// Raises the mouse exit event.
/// when the mouse exits the textmesh`s collision area
/// </summary>
void OnMouseExit()
{
/// return colour to normal white faded alpha
ChangeColor (mat1);
}
/// <summary>
/// Changes the color.
/// change colour method taking a material in as the argument
/// </summary>
/// <param name='mat'>
/// Mat.
/// </param>/ <summary>
/// Changes the color.
/// </summary>
/// <param name='mat'>
/// Mat.
/// </param>/ <summary>
/// Changes the color.
/// </summary>
/// <param name='mat'>
/// Mat.
/// </param>//
void ChangeColor(Material mat)
{
/// if not mat1 or mat 2
if(mat != fadedOutMat && mat != mat2 && mat != mat3)
{
/// mat must be mat 1
mat = mat1;
}
else if(mat != fadedOutMat && mat != mat1 && mat!= mat3)
{
/// mat must be mat 2
mat = mat2;
}
else if(mat != fadedOutMat && mat!= mat1 && mat!= mat2)
{
/// mat must be mat 3
mat = mat3;
}
else
{
/// mat must be mat 4
mat = fadedOutMat;
}
/// assign new material to the textMesh being operated on
txtMesh.renderer.material = mat;
}
/// <summary>
/// Starts a narrative.
///
/// </summary>
/// <returns>
/// The intro talk after time
/// </returns>
IEnumerator StartIntroTalk()
{
audio.clip = narrative;
audio.Play();
if(isClicked)
{
ChangeColor (fadedOutMat);
}
yield
return
new WaitForSeconds(wait);
}
/// <summary>
/// Loads a level based on a mouse input to start game
/// </summary>
/// <returns>
/// The A level.
/// </returns>
IEnumerator LoadALevel()
{
audio.PlayOneShot(beep);
if(isClicked)
{
ChangeColor (fadedOutMat); /// txtMesh.renderer.material.color.a = new Color(txtMesh.renderer.material.color.r,txtMesh.renderer.color.g,txtMesh.renderer.color.b,0.0f);
}
yield
return
new WaitForSeconds(wait);
Application.LoadLevel(levelToLoad);
}
}/// end class
Anwyay. Not a complete answer, but a good place to start from.
take care and let me know if you have any problems getting script to work (it`ll only be something simple to do with setup i stated above )
Take Care and thanks for reading
Gruffy
edit:29.03.2014
Hey bud, here is a package, unzip it, then import it to your project.
make a new scene and then drag the “GUIControlObject” prefab to your Main Camera object.
The press play and mouse over or click them - also have your console window visible when playing.
Take care bud, and sorry again I did not get back very quick, I honestly didn`t realise you had commented further on your problem.
[24477-textmeshmouseovers.zip|24477] <—Please download this