Search Unity

Confused about using enum in inspector.

Discussion in 'Scripting' started by ptgodz, Sep 28, 2016.

  1. ptgodz

    ptgodz

    Joined:
    Aug 4, 2016
    Posts:
    106
    Hi everyone,

    I have a Weapon controller script which has an enum with the following values

    Code (CSharp):
    1. public enum WeaponUpgrades {PISTOL, SHOTGUN,  MACHINEGUN};
    I've declared this outside of the class.

    On another script which I've named UpgradeManager I have the following
    Code (CSharp):
    1. public WeaponUpgrades upgrade;
    2.  
    3. public void BuyWeapon()
    4. {
    5.        Debug.Log("Enum value passed in is: " + upgrade.ToString());
    6. }
    In the inspector I am attaching the UpgradeManager script to each UI button so that I can use the enum in a drop down list and pick the relevant weapon for each button.

    However when I run the above code and look in the console. I find that that no matter what enum I select in the inspector I get the same result which is

    Enum value passed in is: PISTOL

    Does anyone know why this is happening? I thought by assigning the enum in the inspector it would be stored in my upgrade variable and then I could just convert to a string

    Thanks

    EDIT: Just to note there is more code that written there, I've just took what I thought was relevant.
     
  2. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,332
    It should work as stands. The bug is probably somewhere else.
     
  3. Vedrit

    Vedrit

    Joined:
    Feb 8, 2013
    Posts:
    514
    I understand your post as "I've created an enum in another script and setting it to something, then on a different object I'm doing this:
    "create enum,
    print the enum string to console"
    and it always returns the default value."

    If I understand correctly, try this instead
    Code (csharp):
    1. public weaponController controller;
    2. public void BuyWeapon()
    3. {
    4.     Debug.Log("Enum value passed in is: " + controller.upgrade.ToString()); //Or whatever you've called the
    5. }
     
  4. ptgodz

    ptgodz

    Joined:
    Aug 4, 2016
    Posts:
    106
    I've solved it and feel a right idiot. I had the same script attached to another object so I could use another function for a different button, sure enough the value of the enum was being set in that second instance. Woops. Thanks for help Baste, I was starting to go mad.

    Edit: @ Vedrit, No I was setting the enum variable in another script. All sorted now :D, thanks for taking the time time to reply man