change enum value through variables via other script in c#

Hello,
Is it possible to do something like this ?

//File A
public MyEnum { choice01, choice02}

//File B
public class theMixer: MonoBehaviour {
public MyEnum enum01;
}

//File C
public class theSelector: MonoBehaviour {
public MyEnum enum02;

void Update() {
gameObject.GetComponent<theMixer>().enum01 = enum02; //This doesn't work :(
}
}

My workaround is to make a lot of switch using :

gameObject.GetComponent<theMixer>().enum01 = MyEnum.choice01;

But it add tons of useless lines cause I want to define the enum value through the inspector.
Thanks for your help.

as in it doesn’t do anything, or does it throw an error?

You need a cast, GetComponent does not as standard return the type you put inside the “<>” which is kinda stupid since generic typing should work fine… oh well, for it to work try this:

((theMixer)gameObject.GetComponent<theMixer>()).enum01 = enum02;

the first thing you do is to close it all in parenthesis and then the object you wish to cast it to inside parenthesis and woollaa!!!

Hummm… crazy… I wanted to grab the error message (it was a converting enum and missing cast) but the error doesn’t shows up anymore. And it is working now ! The only thing I changed since then is removing the script from the gameobject and reattached it because a string field was not working in the inspector.
Is it possible that could be some kind of bug ?
Sorry but thank you because without that post I would still have a long useless script :sweat_smile:

Ah ! So I’m not crazy !! But how come it does work now ?
Thanks jackie0100 I’ll try your solution to be sure that I won’t have the error in the futur.

Im not sure about that, other seems to have random problems with fields not showing up, search google and unity may be the best way to go, remember, for it to show up it MUST be a public field (from my knowlegde). if this still wont work try

[System.Serializable]
public string myString;

Thank you Jackie