how to set multiple states in scripting unity

hi, How to set multiple states in unity. like say, state1 state2 state3.
Is there an enumeration data type.
Thanks.

With C#, use “enum”.

JavaScript has enum too.

enum Commands {patroll, stay, run};
var command : Commands = Commands.stay;
...

!! Wool Cool !!
Hey Dart, thanks man.:slight_smile:

Is there a way to assign default values. Say patrol = -1, I tried that and patrol: 0 but did not work.

How can I use 1 enum across multiple scripts? (JS)

Hi

Create it in a script that you import in all your other scripts.

In the first lne of each script, write :

import [nameofthescript without path and extenion];

I have another question myself : what is the difference between Array and Enum ?

Thanks.

Enum is like a set of constants. Enum maps names to numbers. You cannot change Enum at the runtime. E.g.

Enum State
{
start = 0,
run = 1
}

you cannot say: State.start = 2; at the runtime, but you can say states[State.start]…

Hope you understand what I meant.

Talking about enums… I don’t understand one thing.

Lets say I have a script that has

Enum States
{
  state_one = 1,
  state_two = 2
}

and I have 2 more scripts: S1 and S2. In both scripts I have imported Enums.js and I use it in S1:

var state : States = States.state_one;

but when I want to use state variable from S1 in S2:

if (s1.state == States.state_one)
{
}

I get an error saying that left hand object of type System.Object cannot be used with right hand object of type States. Why? And how to do this :slight_smile:

This must sound very stupid, but I’m so used to C++ where you can do anything that can be done once you have a reference to an object.

Is “s1” and its class strongly typed?

hmm now that you mentioned it… :slight_smile:

Well I guess it is not. It is just a javascript created from the project panel and added as a component. Didn’t know that you can strongly type it?

I should read all the documentation… but I wanted to dig into the unity faster :slight_smile:

I thought this answered my problem since I have an enum at the start of one script, but that script gets called on each level of my project and Unity keeps telling me that it has already been defined. So I used the Import line, but I can’t get it to call my now-separated “enumLevel” script with any syntax I add. I started with your square brackets (Import [enum]), then tried (), then “”, then (“”) and so on. If I search the Unity docs for Import it returns 1272 items none of which are just “Import” seemingly. Please guide me…

B

Just changed that Import line to:

var levelName = GetComponent("enumLevel");

which now gives me the same “BCE0132: The namespace ‘’ already contains a definition for ‘theLevels’.”

Aaarrgh.

B

A great friend (MentalFish on here) has helped me. Rather than using enum, just creating a variable in my OnTouch script makes it all work.

B