Accessing enum from another script

I want to access an enum I have on another script. I see a common solution is to make it a static, but this won’t work because I’m using it on multiple game objects.

Basically I have two state machines that are layered on one another.

One decides the general state (engage the player, disengage the player, search for the player, return to the start position, and dead) while the other picks specific actions to be used during these general states (move, jump, chase, dodge, attack).

So I need to pull the general states onto the script giving specific actions.

Is there a better method I should be using here?

Here’s the enum on the general states script:

public class Enemy_Stats : MonoBehaviour {

    public enum State { DisengagedState, EngagedState, SearchState, ReturnState, DeadState };
    public State myState;

And here’s the script that I want to access it on:

public class Enemy_Large_Spider : MonoBehaviour {

    public float movementSpeed;
    public float turnSpeed;

    private Vector3 targetPosition;

    public int damage;

    public enum _State{ IdolState, MovingState, ChaseState, DodgeState, JumpState, ShootState };
    public _State myState;

I’m half tempted to use a byte instead and just make note on what each number corresponding state is, but I would really like to learn how to do it the “correct” way.

Any suggestions?

I am not sure what exactly you are asking but enums do not need to belong in any class, they can be global:

public enum GeneralStates
{
    DisengagedState,
    EngagedState,
    SearchState,
    ReturnState,
    DeadState
};

public enum ActionStates
{
    IdolState,
    MovingState,
    ChaseState,
    DodgeState,
    JumpState,
    ShootState
};

Once again though, not sure what youre asking just throwing some information out there.

Right, either move the declaration out of the class, or access it from another class as Enemy_Large_Spider.State.IdolState etc.

But if I move it out of the class then it will effect every gameobject holding the script, wouldn’t it?

// if it's on a different GameObject:
Enemy_Stats stats = target.GetComponent<Enemy_Stats>();

// if it's on the same GameObject:
Enemy_Stats stats = GetComponent<Enemy_Stats>();

Debug.Log(stats.myState);