I have a script which generates a variable, and I would like to use this variable in another script. The problem I am having is that in the other script, the variable needs to be used in the start method, meaning the value of the static variable must be set before the script runs (or at least before the start method runs!). Is this possible? Here are the generator script and the script which uses the variable in the start method (in the instantiate function) respectively. Thanks in advance!
[SyncVar]
public int hostCard;
[SyncVar]
public int clientCard;
// Use this for initialization
void Start () {
if (isServer)
{
hostCard = Random.Range(0, 52);
clientCard = Random.Range(0, 52);
GameplayTest.card = hostCard;
}
if (!isServer)
{
GameplayTest.card = clientCard;
}
}
public static int card;
private GameObject cardParent;
private Vector3 screen;
private GameObject instance;
// Use this for initialization
void Start () {
cardParent = GameObject.FindWithTag("CardParent");
screen = Camera.main.ScreenToWorldPoint(new Vector3(Random.Range(0, Screen.width), Random.Range(0, Screen.height), 20));
instance = Instantiate(Resources.Load(card.ToString(), typeof(GameObject)), screen, Quaternion.identity, cardParent.transform) as GameObject;
}
public void MoveCard()
{
instance.transform.position = Camera.main.ScreenToWorldPoint(new Vector3(Random.Range(0, Screen.width), Random.Range(0, Screen.height), 20));
}
}
If you don’t need to use any part of UnityEngine in order to initialize a static variable, you can initialize it in a static constructor.
For Unity-related stuff (e.g. if it involves GameObjects, MonoBehaviours, or any function in the UnityEngine namespace), the usual technique is for objects to initialize themselves in Awake() so that other objects can safely reference them from Start(). (All objects Awake before any object Starts–assuming all objects are present in the scene, active, and enabled.)
Of course, note that Awake() and Start() are both instance methods, so they will run once per copy of your object in the scene, even though the static variable exists exactly once whether you have 20 copies or 0 copies of the object in your scene.
If it’s not possible to calculate the value of the variable in the first frame of your scene (e.g. because you need to wait for data to be downloaded from a network location), then you’ll have to restructure your code so that any functions that need that variable wait to run until it becomes available.
That’s a very general question, but as a general answer: Don’t put it in Start(). Put it in some new function you make up, and then call that function at the time you want it to run (presumably right after you initialize the data that it’s going to need).
Organizationally, there’s a number of ways of doing that. You could use events, you could use polling, you could use promises, you could just hard-code the function that has the data to call the function that needs the data. This is the big challenge of asynchronous programming and there’s a number of philosophies on how to tackle it.
There are also ways of telling it to wait for N seconds and then run, but that’s almost always a terrible plan, because you don’t know how long is actually long enough. Guess too high and you’re sitting around doing nothing for no good reason; guess too low and your program breaks.
Okay, thanks a lot! I’ve come to the conclusion that the best way to do it for me is to use the update method to wait until the variable has been changed, so thanks for guiding me away from Start()!