Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Reference and set enum from other scripts

Discussion in 'Scripting' started by Denisowator, Mar 8, 2019.

  1. Denisowator

    Denisowator

    Joined:
    Apr 22, 2014
    Posts:
    918
    I have scriptA with the following:
    Code (CSharp):
    1. public enum ThisEnum {
    2.     Item1,
    3.     Item2,
    4.     Item3
    5. }
    6.  
    7. public ThisEnum thisEnum;
    And I have two other scripts. In one I'm trying to set the enum's value, in the other I'm trying to check against it.

    ScriptB has:
    Code (CSharp):
    1. scriptObject.GetComponent<ScriptA>().thisEnum = scriptObject.GetComponent<ScriptA>().thisEnum.Item1;
    Getting this error: Static member `ScriptA.ThisEnum.Item1' cannot be accessed with an instance reference, qualify it with a type name instead

    ScriptC has:
    Code (CSharp):
    1. if (ScriptA.thisEnum == ScriptA.thisEnum.Item1) {
    2. }
    Getting the same error.

    I tried changing the capitalization in all sorts of ways, but that just gives me other errors. I've looked around a ton, googled Forums and Answers posts, looked into Unity documentation. Can't find anything.
     
  2. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,616
    if (ScriptA.thisEnum == ScriptA.thisEnum.Item1)


    should be

    if (ScriptA.thisEnum == ScriptA.ThisEnum.Item1)

    ThisEnum is the name of your Enum and thisEnum is an instance variable. It might be less confusing if you give them different names.

    By the way, this is a C# feature, rather than a Unity feature, so it may be that Unity doesn't document it anywhere. You would want to look-up C# references instead.
     
  3. Denisowator

    Denisowator

    Joined:
    Apr 22, 2014
    Posts:
    918
    I tried setting the second part to the enum itself. I get this error:
    `ThisEnum': cannot reference a type through an expression. Consider using `ScriptA.ThisEnum' instead

    Code (CSharp):
    1. if (ScriptA.thisEnum == ScriptA.ThisEnum.Item1) {
    2. }
     
  4. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,616
    Are you sure that's the line that the error is referring to? You are clearly using ScriptA.ThisEnum already.
     
  5. Denisowator

    Denisowator

    Joined:
    Apr 22, 2014
    Posts:
    918
  6. Denisowator

    Denisowator

    Joined:
    Apr 22, 2014
    Posts:
    918
    Restarted Unity, no errors anymore. o_O
     
  7. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,616
    It just occurred to me that ScriptA.thisEnum does not make any sense unless you have an instance of ScriptA that's also called ScriptA. Is that the case? That might also cause confusing errors.
     
  8. Denisowator

    Denisowator

    Joined:
    Apr 22, 2014
    Posts:
    918
    Yeah "ScriptA" is an instance name for a reference to ScriptA.
     
  9. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,616
    I would name it something different. I think technically the instance name ScriptA will occlude the type name ScriptA and if you want to use the enum, then you'll need access to both the instance and the type.
     
    Denisowator likes this.