Friday, July 4, 2014

Creating global variable in Unity3D

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.

3 comments:

  1. Thanks for this, Unity does not explain this well at all.

    ReplyDelete
  2. Sir, I would like to ask you kindly for a request. Can you update this for Unity 5? I am getting errors using your code. Also can you explain how the " background object" part works, can I use any object. Thanks!

    ReplyDelete