Search Unity

  1. How can our website serve you better? Give us your feedback. Take our survey and let us know.
    Dismiss Notice

Changing enum from another script

Discussion in 'Scripting' started by davidnibi, May 20, 2021.

  1. davidnibi

    davidnibi

    Joined:
    Dec 19, 2012
    Posts:
    342
    Can someone explain how to access and change enums from another script:

    My enum:

    Code (CSharp):
    1.     public enum ForkFunction // fork operation
    2.     {
    3.         forkOperateStop,
    4.         forkOperateUp,
    5.         forkOperateDown,
    6.         forkOperateShake
    7.     }
    8.  
    9.     public ForkFunction forkFunction;
    I'm altering these using switch, which works fine.

    I'm accessing this through another script using this:

    Code (CSharp):
    1. public Transform truckForks;
    2. public ForkController _forkControllerScript;
    3. _forkControllerScript = truckForks.GetComponent<ForkController> ();
    4.  
    5. _forkControllerScript.forkFunction = ForkFunction.forkOperateStop;
    The last part returns an error:

    error CS0103: The name 'ForkFunction' does not exist in the current context

    I've not used enums a lot, unfortunately, I've run out of ideas. I'm changing it in the forkcontroller script this way ...
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    16,832
    ForkFunction is a nested enum within another class.

    If you do this, then it needs to be preceded by the name of the class it is defined in when used from outside that class.

    MyClassName.ForkFunction.forkOperateStop
     
    davidnibi likes this.
  3. davidnibi

    davidnibi

    Joined:
    Dec 19, 2012
    Posts:
    342
    Thanks, that makes sense, , it worked. ;) I tried to reference it with _forkControllerScript prior.
     
    Kurt-Dekker likes this.
unityunity