Search Unity

Change a enum value from another script

Discussion in 'Scripting' started by Griffo, Feb 12, 2016.

  1. Griffo

    Griffo

    Joined:
    Jul 5, 2011
    Posts:
    700
    Hi, I want to access and change a enum value from another script, I thought this might work.

    Code (JavaScript):
    1. #pragma strict
    2. private var enumModeStatus : testScript.Mode;
    3. function Awake ()
    4. {
    5.      enumModeStatus = transform.root.GetChild(1).gameObject.GetComponent.<testScript>().Mode;
    6. }
    7. function Start ()
    8. {
    9.      enumModeStatus.Car;
    10. }
    But I get the error ..

    Cannot convert 'System.Type' to
     
  2. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    don't use GetChild(1) as the index is not always the same and not matching hierarchy.

    i don'T know if you can assign a enum to an var defined variable.

    can you post your testscript too , an maybe the rest of the error massage ?
     
  3. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    Code (csharp):
    1.  
    2. // shouldnt this...
    3.  
    4. enumModeStatus = transform.root.GetChild(1).gameObject.GetComponent.<testScript>().Mode;
    5.  
    6. // be this?
    7.  
    8. enumModeStatus = transform.root.GetChild(1).gameObject.GetComponent("testScript").Mode;
    9.  
     
  4. Griffo

    Griffo

    Joined:
    Jul 5, 2011
    Posts:
    700
    Thanks for the replies, @martinmr I can access all the var's on the testScript just not enum.

    I now know enums are not specific instances of anything HERE

    But I still can't get access to it, if I Debug.Log ref to any var's on the testScript they show as expected but not the enum

    @bigmisterb it is correct as @martiman says.
     
    Last edited: Feb 12, 2016
  5. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
  6. Griffo

    Griffo

    Joined:
    Jul 5, 2011
    Posts:
    700
    All my GetComponent ref in thousands of lines of code are ref like.<>() and all work ;)

    I'm sure in one of the updates they said to use <>() instead of ()

    Must be able to use both ways, like I said the only difference from javascript to C# is the . before the <>()
     
    Last edited: Feb 12, 2016
  7. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    I deleted my comment with the javascript because was unsure as i had in mind that there is something with
    GetComponent.<T>() in US/JS standing in the documentation of unity, but as i have seen today that it got lost fromt there. Maybe they updated the JS page for GetComponent.

    If you just could show how you have initiated your enum ?

    is "Mode" your enum declaration ?

    like

    enum Mode {Car, Tank, House, Shoe }
     
    Last edited: Feb 12, 2016
  8. Griffo

    Griffo

    Joined:
    Jul 5, 2011
    Posts:
    700
    Code (CSharp):
    1. enum Mode{Car, Bus, Bike};
    2.     var choiceMade : Mode;
    This is just in a blank testScript using for testing because I can't get ref to a enum in a big script.
     
  9. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    than this

    enumModeStatus = transform.root.GetChild(1).gameObject.GetComponent.<testScript>().Mode;

    is causing an error as you have a variable enumModeStatus of type Mode (which is your enum) so you can know assign values like

    (also i would write enums in UpperCase letters) enum MODE {Car, Bus, Bike};

    enumModeStatus = testScript.MODE.Car;
    enumModeStatus = testScript.MODE.Bus;
    enumModeStatus = testScript.MODE.Bike;

    for example:

    Code (JavaScript):
    1. #pragma strict
    2. private var enumModeStatus : testScript.MODE;
    3. function Awake () {
    4.      enumModeStatus = testScript.MODE.Car;
    5. }
    6. function Start () {
    7.      if(enumModeStatus == testScript.MODE.Car){
    8.           enumModeStatus = testScript.MODE.Bus;
    9.      }
    10. }
    if you are setting in the testscript the value of choiceMade already to one of your enum values,
    and now want to take ot from the testScript.


    Code (JavaScript):
    1. #pragma strict
    2. private var enumModeStatus : testScript.MODE;
    3. function Awake () {
    4.      enumModeStatus = transform.root.GetComponentInChildren(testScript).choiceMade
    5. }
    6. function Start () {
    7.      if(enumModeStatus == testScript.MODE.Car){
    8.           //DO stuff here
    9.      } else if (enumModeStatus == testScript.MODE.Bus) {
    10.           //DO stuff here
    11.      } else if (enumModeStatus == testScript.MODE.Bike) {
    12.           //DO stuff here
    13.      }
    14. }
    (don't know if my examples work with that GetComponentInChildren i'm not in JS/US)

    But you shouldn't use a fix Index with GetChild() because it won't match every time as the Hierarchy position is not same as the index

    EDIT: there was one little mistake from my side, changed

    Code (JavaScript):
    1. if(enumModeStatus == testScript.MODE.Car){
    2.           enumModeStatus.Bus;
    3.      }
    4.  
    5. //to
    6.  
    7. if(enumModeStatus == testScript.MODE.Car){
    8.           enumModeStatus = testScript.MODE.Bus;
    9.      }
     
    Last edited: Feb 12, 2016
  10. Griffo

    Griffo

    Joined:
    Jul 5, 2011
    Posts:
    700
    @martinmr thanks for the help. I'll take a look at this and get back to you, need to pop out for a while now, cheers.
     
  11. Griffo

    Griffo

    Joined:
    Jul 5, 2011
    Posts:
    700
    @martinmr Great I finally got it with your help, I see my mistake now, I was trying to access MODE instead of choiceMade.

    Much appreciated.
     
    martinmr likes this.