- Home /
Toggle between GUITextures
So here's my problem, I have two buttons, and i need one to be highlighted while the other is not, simple as that. If i select a non highlighted button it will be set active and highlight while setting the other button non active and thus not highlighted.
For the life of me i cannot get the colors to change. It will set the bool to true or false but in the colorChanger method the color wont change and the debug wont show.. I'm certain its my lack of programming experience.
I'm terrible at c# or any programming language so bear with me..
Here is what I have.
Color changing script- attached to both buttons.
public bool colorChange = false;
public Color buttonColor;
public Color activeButtonColor;
void start{
}
void OnMouseDown () {
colorChange = true;
//This changes opposite buttons "colorChange" bool to false..
GameObject object1 = GameObject.Find("mygameobject");
object1.GetComponent<mygameobjectScript>().colorChange = false;
}
void colorchanger () {
if(colorChange){
guiTexture.color = activeButtonColor;
Debug.Log ("color change true");
}
else{
guiTexture.color = buttonColor;
}
}
}
I tried to make it clear sorry if this is utter crap I'm new to this. Thanks for any help.
Answer by robertbu · Aug 27, 2014 at 03:02 PM
I suspect your problem is with GameObject.Find(), but rather than work though how you have things structured, I'm going to suggest an alternate solution. 'static' variables are shared by a class. If you read enough posts on UA, you will find dire warnings about using static variables, but I find they are 'safe' and effective communicating between instances of a class. So here is a rewrite of your code:
using System.Collections;
using UnityEngine;
public class Example : MonoBehaviour {
private static Transform active = null;
public Color buttonColor = Color.white;
public Color activeButtonColor = Color.red;
void Start() {
guiTexture.color = buttonColor;
}
void Update() {
if (Input.GetMouseButton (0)) {
if (active != transform) {
guiTexture.color = buttonColor;
}
}
}
void OnMouseDown () {
active = transform;
guiTexture.color = activeButtonColor;
}
}
This rewrite will work with any number of GUITextures with this script.
Answer by Oatz · Sep 03, 2014 at 05:48 AM
Figured it out. Thanks for trying though I don't think I was very clear.
Here is what I came up with for toggling between two buttons.. With the script attached to each button, the script will change the bool "colorChange" on the opposite button.
public bool colorChange = false;
public Color buttonColor;
public Color activeButtonColor;
void Update(){
if (colorChange == true){
guiTexture.color = activeButtonColor;
}
else{
guiTexture.color = buttonColor;
}
}
void OnMouseDown (){
colorChange = true;
if(colorChange){
GameObject buttonOne = GameObject.Find("Button One");
buttonOne.GetComponent<ButtonOneScript>().colorChange = false;
}
}
}
Your answer
Follow this Question
Related Questions
Toggle Button Question 0 Answers
spawn and destroy with toggle button? 1 Answer
Toggle Group event called 2 times Unity UI 1 Answer
Toggle full screen double click issue 2 Answers
Reduce Draw call for Multiple GUI Textures with same Texture 1 Answer