Can't get my Enum variables to change from another Class,...

I’ve spent most of the day( unbeknownst to my boss :p) googling for answers,…which I found some that sound good,…but doesn’t seem to work when I apply it.

My situation (simplified example)

Instantiate 3 cubes, each with two scripts, EnumScript and PassValue, on the same object.

EnumScript

public class EnumScript : MonoBehaviour {
	
	
	public enum PlayerState{offense,defense,neutral}
	public PlayerState statusP;
	
	void Start () {
		statusP = PlayerState.neutral;
	}
	
	
	void OnMouseOver()
	{
		Debug.Log("State ==== " + statusP);  // checking to see if anything changed
	}

PassValue

public class PassValue : MonoBehaviour {
	

	void Start () {
	}

	void OnMouseDown()
	{
		EnumScript enumScript;
        enumScript = GetComponent<EnumScript>();
		//enumScript.statusP = PlayerState.defense;    // fail
		EnumScript.PlayerState statusP = EnumScript.PlayerState.defense; // trying to change the enum
	}

I want each cube to have it’s own enum state(offense,defense,neutral) when I click it,…and not be global across all of them.

Which I can get to work fine IF I change it within it’s own Class, for example. I can also pass other variables like Bool and INT no problem.,.it’s just enums are being a pain.

Currently, the above script does Nothing,…no errors ,…just nothing but debug it’s default state(neutral) on MouseOver.

If anyone has a solution to apply to this example, I’d be very appreciative!

Also…while we’re talking about Enums,…do they play nice as a Singleton? :slight_smile:

THANKS!
-Welby

You don’t need to use GetComponent or anything; enums are global (within a namespace anyway).

–Eric

Yeah,…I tried that too,…which is why,…

EnumScript.PlayerState statusP = EnumScript.PlayerState.defense; I am using the Capitol "E’ ,…but even still if I lose the GetComponent part

this script doesn’t change the enum in the EnumScript Class. :frowning:

thanks

Script A…not attached to anything:

enum Foo {A, B}

Script B:

using UnityEngine;

public class Test : MonoBehaviour {
	void Start () {
		var foo = Foo.A;
		print (foo);
	}
}

–Eric

js?! oh noes!,…hehe…j/k

I do appreciate the response,…and yes, it does indeed change the enum in your script A.

But not having it attached to my instantiated cubes doesn’t do anything for them. As I mentioned, each cube should have it’s own independent state(offense, defense,neutral), and when I click it,…it should change that state for ONly that cube, not globally across all 3 instantiated cubes.

make sense? sorry for not being clearer.

-Welby

lemme tinker some more before I jump the gun,…I do not doubt you are telling me the right thing,…it’s my comprehension that is questionable :stuck_out_tongue:

EDIT:

I am not familiar with JS,…

what would var foo be in C#?

what type should I use for Public “”" foo;

I thought it would be whatever I called my enum,…but it’s not liking it…

Er…no. C#.

Have the enum set up in a separate script, not part of EnumScript (although a better name than EnumScript would be ideal…). You can do

void OnMouseDown()
{
    GetComponent<EnumScript>().statusP = PlayerState.defense;
}

If that doesn’t work, then the GetComponent is failing.

–Eric

opps…my bad,…the Var threw me off for some reason :stuck_out_tongue:

thanks I will try this,…

-Welby

Bleh,…I’ve had enough of this for today,…not working,…

my EnumScript is sitting in the Project panel, not attached to anything,…I got the PassValue script with the above MouseDown code, on my instantiated objects,…but giving me,…

Assets/PassValue.cs(12,54): error CS0103: The name `PlayerState’ does not exist in the current context errors…

Which makes ‘sense’ to me logically,…since it’s looking for something not attached to anything,…does it even know it exists?!

Loose Script

public class EnumScript : MonoBehaviour {
	
	
	public enum PlayerState{offense,defense,neutral}
	public PlayerState statusP;
	
	void Start () {
		statusP = PlayerState.neutral;
	}

Script2 on all three of my instantiated objects.

public class PassValue : MonoBehaviour {
	
	void Start () {
	}

	void OnMouseDown()
	{
		Debug.Log("Pressed");
		GetComponent<EnumScript>().statusP = PlayerState.defense;
	
	}

What is it about enums that don’t play nice like the other types? Is my instantiation messing things up?

anyways,.,thanks for the help,Eric,…I won’t bother you anymore,…this can wait.

I will go home and try experiments,…like not using instantiated objects,…just try it with one already in the scene.

thanks for your time.

-Welby

Your enum should be declared in a separate script, not attached to anything, because enums are not specific instances of anything. (Think of all the Unity enums like Space, CollisionFlags, TextureFormat, etc. etc.) A variable, however, is a specific instance of something and should be attached to an object. You should have 3 scripts, not 2.

–Eric

Just an upDate in case anyone else finds this useful. I got it to work the way I wanted.

Thank you, again, Eric for helping me…I was looking at enums and what they are the wrong way, that last description you gave helped. Once it clicked.,.I knew How to use it. :razz:

/cheers!

Three Scripts: one declaring the enum, one on my instantiated cubes with an enum variable, and another on the same cubes to switch it.

Script1: StatesScript ( just floating around not attached to any Object)

public class StateScript : MonoBehaviour {

	public enum PlayerState{offense,defense,neutral}

	void Start () {
	}

}

Script2: ( on all the instantiated Cubes, each with it’s own enum state)

public class TouchScript : MonoBehaviour {
	

	public int charNum;
	public StateScript.PlayerState statusP;

	void Start ()
	{
		renderer.material.color = Color.gray;
	}


	void OnMouseOver()
	{
		Debug.Log("STATE=  " + statusP);
	}

Script3: ( also attached to the cubes, when clicked, changes the enum, on Script2, of the Cube clicked Only)

public class PassValue : MonoBehaviour {

	void Start () {
	}

	void OnMouseDown()
	{
		Debug.Log("Pressed");
		GetComponent<TouchScript>().statusP = StateScript.PlayerState.defense;
	
	}

}
1 Like

Actually your StateScript can just be

public enum PlayerState{offense,defense,neutral}

It doesn’t need to be in a class or anything.

–Eric

well, whaddya know,…works too! a whole script for one enum,…seems like a waste of paper :stuck_out_tongue: so to speak.

Less words though,…I like it.

I’ll take it though,

Put some more enums in that script. :wink:

–Eric

1 Like

you might be able to help me with this i am trying to make an enum change when something is selected in another enum here is my sample script

#pragma strict
@script ExecuteInEditMode()

public enum menuListA
{aa = 0,ab = 1,ac = 2,}
public enum menuListB
{ad = 3,ae = 4,af = 5,ag = 6,ah = 7,}

var menuA : menuListA;
var menuB : menuListB;

function Update(){

if(menuA == menuListA.aa){

Debug.Log(“menuB = 5 to 7”); // this is the bit im really stuck on getting menu b to only a few options
}

}