I have two static scripts but only one is successfully being accessed by other scripts, and I’m not sure what I’ve done wrong/differently.
My GameManagerScript can be accessed by all my other scripts without problem (as below).
public class GameManagerScript : MonoBehaviour {
public static GameManagerScript gamecontrol;
public float money;
void Awake () {
if (gamecontrol == null) {
DontDestroyOnLoad (gameObject);
gamecontrol = this;
}
else if (gamecontrol != this) {
Destroy (gameObject);
} }
Accessing script example
public class Financesscript : MonoBehaviour {
public Text moneytext;
void Start () {
Moneyupdate ();
}
void Moneyupdate () {
print ("Money Updated");
moneytext.text = "$" + GameManagerScript.gamecontrol.money;
}
public void ReduceMoney () {
GameManagerScript.gamecontrol.money -= 10;
Moneyupdate ();
}}
But my SceneManager (2nd static script) isn’t working in-game. While writing the accessing scripts, MonoBehaviour is picking it up fine (ie. is providing the variables in the quick-finish drop down list), but when I try to play the game I get an ‘Object Reference Not Set To An Instance Of An Object’ error. The same script can access my GameManagerScript but not my SceneManagerscript, which means it’s an error in SceneManagerscript that is causing the problem. But I can’t find it…anyone know what I’ve done wrong?
using UnityEngine;
using System.Collections;
public class SceneManagerscript : MonoBehaviour {
public static SceneManagerscript scenecontrol;
public float country;
public bool antarcticaselected;
public bool denmarkselected;
void Start () {
antarcticaselected = false;
denmarkselected = false;
print ("All Bools set to false");
} }
Accessing script
public class CountrySelection : MonoBehaviour {
void OnMouseDown() {
Debug.Log ("antarctica is selected");
GetComponent<SpriteRenderer> ().color = Color.red;
SceneManagerscript.scenecontrol.antarcticaselected = true;
}}