- Home /
I have doubt please help me
I have two buttons button-A and button-B. If button-A in active position then button-B in detective position vice versa. please help me in code in java script unity. I have no ideas coming till now.
You Haven't really asked a question... I don't know what you mean by "detective" position
I think you mean Active state and Normal state. Button are not too suited for this , use GUI.Toggle with a button style.
var a = false
function OnGUI() {
a = GUI.Toggle(Rect(0,0,80,20), a , "A", GUI.skin.button);
var b = !a;
GUI.Toggle(Rect(0,30,80,20), b , "B", GUI.skin.button);
}
@ramjigeddam - please watch a video tutorial and learn how to properly format the code you post.
For what I understand he wants to activate and deactivate buttons. So you can't press both...
Could you edit the comments so everything is outlined and readable.
Answer by zVolodymyr · Nov 25, 2013 at 11:43 AM
Something like that?
if(GUI.Toggle (new Rect(5, 70, 100, 30), canRotate, "Activate"))
canRotate=true;
if(GUI.Toggle (new Rect(5, 70, 100, 30), !canRotate, "Deactivate"))
canRotate = false;
Answer by Bunny83 · Nov 25, 2013 at 10:46 AM
Not sure if i got your question right. So you want two buttons like a radio-button-group where one 1 active the other not. The easiest way is to use Toggles. Depending on your desired behaviour one of those:
//version1
function OnGUI()
{
if (GUI.Toggle(new Rect(5, 70, 100, 30), canRotate, "Activate", "Button"))
{
canRotate = true;
}
if (GUI.Toggle(new Rect(5, 110, 100, 30),!canRotate, "Deactivate", "Button"))
{
canRotate = false;
}
}
//version2
function OnGUI()
{
canRotate = GUI.Toggle(new Rect(5, 70, 100, 30), canRotate, "Activate", "Button");
canRotate = !GUI.Toggle(new Rect(5, 110, 100, 30),!canRotate, "Deactivate", "Button");
}
Version 1 works like a true radio button group, so when you click one it gets active and if you click again it stays active.
Version 2 are just alternating toggles, so if you press either of the buttons they toggle the state
You probably want version 1.