- Home /
Variable Selector
i remember there being a variable that would allow you to select the options you want... that wasnt very descriptive but ill go further into detail. Once i drag my script onto a game object and than look into the inspector and i should see all of my variables. i want to be able to click one and select different options. as in example, if i was to create a weapon script and i didnt want to create multiple scripts for melee and range instead i could just click the variable and than select melee or range. I know that im probably not answering this question quite clearly but i just cant remember what it is.
Answer by BinaryCaveman · Feb 19, 2011 at 11:02 PM
You might be looking for an enum
.
The Standard Assets/Character Controllers/Sources/Scripts/MouseLook.cs
script makes use of these.
To declare an enum
like in the Mouse Look script, use this (C#):
public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
public RotationAxes axes = RotationAxes.MouseXAndY;
and as also in the Mouse Look script, you can use if
statements to check the current value of the enum
:
if (axes == RotationAxes.MouseXAndY)
// do something
else if (axes == RotationAxes.MouseX)
// do something else
else
// do something else
For JavaScript, it's pretty similar. See this answer for more info.
This good info was posted while I was posting. I guess I'll keep my answer up because my example more closely fits the specific example in the question, not that it matters much, but it could help to clarify.
I agree. I was just giving a simple example of how enums are being used in Unity.
Answer by Jessy · Feb 19, 2011 at 11:08 PM
I think you're talking about an enum. Yes?
import UnityEngine.Debug;
enum Attack {Melee, Range} var attack : Attack;
function Start () { Log(attack == Attack.Melee ? "Melee Attack!" : "Range Attack!"); }
Your answer

Follow this Question
Related Questions
Does Quality Level remain between scenes? 1 Answer
Difficulty Calling Variables from other scrtipts 1 Answer
Passing a set of variables from one script to another 3 Answers
How to use a script on multiple gameobjects, then change a variable for one of them, not the other. 3 Answers
iTween ValueTo not working 1 Answer