- Home /
Changing two different objects renderer colour
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");
}
Thanks in Advance for your help.
Can you post your current code? Just makes it easier to understand what you're trying to accomplish...
Is the color changing at all? Is this on the text or the button (box). Is the box/button a gameobject? Does it have a material attached that has a color parameter? Is the text a GUItext object attached to the button? These things will also help guide you to a correct solution.
Yes the color of the text is changing. Its just a 3D text with a box collider but yes it is a gameobject. The text mesh does have a color parameter. The text then has the script which turns it into a button. So what Im trying to do is basically change two gameobjects color to different colors from one script
Answer by Gruffy · Mar 20, 2014 at 09:46 PM
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. Package here <---Please download this
hey @dok101 how is your solution going ? Are you any further or have you solved it even? Take care bud.
Gruffy
Thanks Gruffy For some reason when I hover over it the entire text just turns into boxes of whatever material Ive selected in the mat. Thanks for your help
sorry bud, i didnt realise you replied to this. whats the problem sorry > ?
by your above comment, im guessing you have not reduced the code to fit yours etc and are experiencing issue with swaps because you have no materials specified in the inspector etc.
to resolve your above issue, make three other materials and add them into your script at the inspector panel, then press play. alternatively, i will be back in 15 $$anonymous$$s, with a package for you to load into unity and press play to see it demoing. take care brb Gruffy
Your answer
Follow this Question
Related Questions
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
Adjust tint color of particles/additive? 2 Answers
Reduce Skid Mark Alpha Gradually 0 Answers