Gamestate, passing object between scene problem

Hi, I’ve got two scenes - “scene1” & “scene2”

I’ve got a empty object called “_myStates” with the script “myStates” attached.
The myStates script has the DontDeleteOnLoad function on it, so it passes over to my other scene.

However i’m having trouble referencing it in my scene2 script, which I just need to recognise it so from in there I can change the GameState (which just goes between freeroam and battle for now)

I tried to find the object using a tag but that only gave me the error
‘Cannot implicitly convert type UnityEngine.GameObject[]' to UnityEngine.GameObject’’

I tried creating the _myStates as a prefab but then I had problems with it being (clone) at the end, and even changing that to be myStates again, my script couldn’t find it… It was a lot of hastle.

Could anyone help me try and reference it?
This is the script from the ‘scene2’ which i’m having the error with.

 GameObject myStatesObject;

	void Awake(){
	myStatesObject = GameObject.FindGameObjectsWithTag("State");
	}

void OnGUI(){
		if(GUI.Button (new Rect(680,250,100,20),"End Battle")){
			myStatesObject.state = MyStates.State.freeRoam;
		}

and this is my myStates.cs (I know it’s a bit hideous and unpractical for the loading scenes but I’ll cross that bridge later)

public class MyStates : MonoBehaviour {
	
	public enum State
	{ 
		freeRoam,
		Battle
	}

	void Awake(){
		DontDestroyOnLoad (this.gameObject);
		state = MyStates.State.freeRoam;
	}

	public State state; //local variable

	IEnumerator Start(){
		while (true) {
			switch(state){
			case State.freeRoam:
					//Application.LoadLevel("scene1");
				freeRoam();
				break;
			case State.Battle:
				Application.LoadLevel("scene2");
				Battle();
				break;
			} //end switch
			yield return 0;
		}//End While loop
	}//End Start
	
	private void freeRoam(){
		Debug.Log ("In FreeRoam");
	}

	private void Battle(){
		Debug.Log ("In Battle");
	}

	

}//End Monodevelop

Thank you for any help!

If you REALLY want to find your state object use the FindObjectOfType function (see documentation[1])

But I’d advise you to change your design and use a simple class (no monobehaviour) using the singleton pattern or just making the attributes static.

[1] Unity - Scripting API: Object.FindObjectOfType

Thank you that did work, but I think your’e right.
After implementing that I did get some very messy functionality.

Sorry for the late reply too, I was looking up the singleton pattern, its a bit daunting for me looking at it, but it does seem to be exactly what I should use.

Thanks for the great answer!