Hi,
I have a couple of booleans, and I want to have only one true. Does anybody have any idea how to do this?
Hi,
I have a couple of booleans, and I want to have only one true. Does anybody have any idea how to do this?
You could either use the Enumerator concept to have a single boolean to store different states.
Or you can write down a function in which you pass an argument as a number.
In that function write down a switch case for that number and depending on the number set a specific boolean to true and others to false right there itself.
right multiple cases for different booleans depending on the number’s value.
But this is just tedious, I would just suggest that you go for the enumerator concept.
Note: if this helps, mark as solved.
If you have several booleans and you only want one to be true at a time, I would suggest using an Enumerator.
This code should be able to demonstrate how an enumerator (enum) and a switch-case could work to your needs.
Create a new scene, then attach the following script to the camera. Hit play, then alternately press one of the mouse buttons or the space bar. Watch the GUI box.
EnumExample.js
#pragma strict
enum AllStates
{
Idle,
LMB,
MMB,
RMB,
Spacebar
}
var myState : AllStates = AllStates.Idle; // or just = 0;
var guiString : String ="";
function Update()
{
GetInput();
DetermineState();
}
function GetInput()
{
if ( Input.GetMouseButtonDown(0) )
{
myState = AllStates.LMB;
}
if ( Input.GetMouseButtonDown(1) )
{
myState = AllStates.RMB;
}
if ( Input.GetMouseButtonDown(2) )
{
myState = AllStates.MMB;
}
if ( Input.GetKeyDown( KeyCode.Space ) )
{
myState = AllStates.Spacebar;
}
}
function DetermineState()
{
switch ( myState )
{
case AllStates.LMB :
guiString = "LEFT mouse button";
break;
case AllStates.MMB :
guiString = "MIDDLE mouse button";
break;
case AllStates.RMB :
guiString = "RIGHT mouse button";
break;
case AllStates.Spacebar :
guiString = "Space Bar";
break;
default :
guiString = "none";
break;
}
}
function OnGUI()
{
GUI.Box( Rect( 20, 20, 200, 30 ), guiString );
}