Sometime you want to create Global object that you can access from whole project.
Recently while working on my unity game, I also needed the same.
While method is quite simple to achieve the same, but its quite confusing if you are just started with Unity3D just like me.
Finally I now understand the process, and in this post I am describing how we can create global variable with Unity3D.
In Unity we work with Scene and when scene is unloaded all its objects are unloaded as well.
To make global variable, we need to create a game object in first scene and attach a script to it which prevent it from unloading when scene is unloaded. Now we can access this game object from newly loaded scene and other scenes as well.
Let's start with simple project and create a scene, I called this scene as first scene.
Now we need to create a empty object which we treat as a global gameobject. This object will not be destroyed when first scene is unloaded.
To preserve this gameobject when First scene is unloading, we need to attach a script, I named it MakeGlobal and added following code to it.
using UnityEngine;
using System.Collections;
public class MakeGlobal : MonoBehaviour {
void Awake () {
DontDestroyOnLoad (transform.gameObject);
}
}
By using DontDestroyOnLoad, we ask Unity engine not to destroy this gameobject when its scene is getting unloaded, now we can assign a new script to this global gameobject, which holds global variables.
This script is simple, it just holds a variable which can be accessed from any scene by any game object.
using UnityEngine;
using System.Collections;
public class GlobalVariables : MonoBehaviour {
public string globalVar;
}
Now we have a GameObject which we can treat as a Global Object and it has a script which holds global variables. But till now we have not used it. To use it we can assign a script to Background element and that assign some value to globalVar variable.
using UnityEngine;
using System.Collections;
public class background : MonoBehaviour {
// Use this for initialization
void Start () {
GameObject globalGO = GameObject.Find("global_gameObject");
GlobalVariables globalVars = globalGO.GetComponent();
globalVars.globalVar = "String from 1st Scene";
}
}
Now our global variable has value which is assigned by script attached to background object. To see its value on screen I created a 3D Text Object and assigned a simple script which update text object's text to global variable.
using UnityEngine;
using System.Collections;
public class TextUpdate : MonoBehaviour {
// Update is called once per frame
void Update () {
GameObject globalGO = GameObject.Find("global_gameObject");
GlobalVariables globalVars = globalGO.GetComponent();
gameObject.GetComponent ().text = globalVars.globalVar;
}
}
Now if you run first scene, you can see text is displaying global variables value.
To test if global variable really retaining its value, we should create another scene and destroy first scene. So I created a another scene, which has 3D text object which displays global variables value, using the same TextUpdate script. You will can see it still preservs value from first scene and we can use global variable in second scene as well.
That's all, Thanks for reading my blog.