My problem is that I have an enum which defines what state the player is in. Everytime the enum changes it does everything I want it to however I’m having problems changing the actual enum state. The way I would like it to work is that everytime they enter a trigger, to change the enum state to the triggers tag. For example as soon as they pass the first trigger it will put them into enum state “one”. This trigger has a tag on it named “one”. I know I could get it to work a couple different ways but is there a way I could take this trigger tag as a string and change the enum state , a psuedo code example would be,
This code above obviosly doesn’t work but is there a way I can do something similiar?
I could accomplish it by changing trigger to the object itself and putting a script on each object that a trigger is on but this would require a lot of scripts and does not seem efficient for what I’m trying to do.
Enum values can both be assigned an integer and referenced as an integer:
//Example with implicit values.
enum Example1 {
State1, // = 0 by default
State2, // = 1 by default
State3 // = 2 by default
}
//Example with explicit values.
enum Example1 {
State1 = 1,
State2 = 2,
State3 = 3
}
If you must use tags, then you can name the tag as the integer value corresponding to the enum state and parse it as an integer to set the enum’s value:
public class Example : MonoBehaviour {
public State currentState;
void OnTriggerEnter2D(Collider2D collider) {
int newState = int.Parse(collider.tag); //Assume the tag is named "1" or "2" or "3", etc...
currentState = (State)newState;
}
}
public enum State {
State1,
State2,
State3
}
I find using tags for this to be a bit messy however. Another way you can go about this is to create a very basic script attached to each of the GameObjects containing the trigger colliders, which simply stores a value of the state for the player to change to when entering the collider:
public class StateAssigner : MonoBehaviour {
public State state;
}
public class Example : MonoBehaviour {
public State currentState;
void OnTriggerEnter2D(Collider2D collider) {
StateAssigner stateAssigner = collider.gameObject.GetComponent<StateAssigner>();
if(stateAssigner != null) {
currentState = stateAssigner.state;
}
}
}
public enum State {
State1,
State2,
State3
}
I wouldn’t really advocate doing it this way- to me, it makes more sense that you have a lookup table with a string ID that says what Enum value the given tag corresponds to.
This means you can just get the enum value by doing mEnumLookup[“SomeTagA”], and you can have multiple tags pointing to the same Enum value, if and when it’s needed.
That said, you can actually do what you ask directly using:
This will check the string “SomeTagA” against the names of the Enum members, instead of the values, then hand you back the Enum value. Be very careful with this- it defeats some of the purpose of enumerations I feel, that the string name is being relied upon this way, but it’s available if you need it.