Reputation: 5246
Let me start by saying I know that using static
is the default method of using global variables. However, it doesn't work for me.
I've got this (very simple) Class:
public class GameSettings
{
public static Boss chosenBoss;
}
Which I set from another class, depending on user input, for example:
using UnityEngine;
using System.Collections;
public class ChooseMort : MonoBehaviour
{
public Boss mort;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
}
void OnMouseOver(){
if(Input.GetMouseButtonDown(0)){
GameSettings.chosenBoss = mort;
Application.LoadLevel("MainGame");
}
}
}
I then attempt to access this variable once the main scene starts like so:
// Use this for initialization
void Start ()
{
boss = GameSettings.chosenBoss;
PositionBoss ();
...
}
However, whenever I get to the boss = GameSettings.chosenBoss
- It's always null
.
I've also tried Singletons
after researching and stumbling on to this post: Singletons
But unfortunately these didn't work for me either. I fear that I'm missing something really simple. Can anyone point out where I'm going wrong?
Thanks.
Upvotes: 1
Views: 82
Reputation: 18805
You are attempting to reference GameSettings
as if it were a static class. ie..
boss = GameSettings.chosenBoss;
and
GameSettings.chosenBoss = mort;
First you need to instantiate that class in order to use it, because you're not using a static class, or singleton.. (which confuses me slightly as I'm not certain as to why you will need multiple GameSettings..)
Anyway,,
public class ChooseMort : MonoBehaviour
{
public Boss mort;
private GameSettings gameSettings;
// Use this for initialization
void Start ()
{
gameSettings = new GameSettings();
}
// Update is called once per frame
void Update ()
{
}
void OnMouseOver(){
if(Input.GetMouseButtonDown(0)){
gameSettings.chosenBoss = mort;
Application.LoadLevel("MainGame");
}
}
}
Upvotes: 1