Friday, 9 May 2014

Global Scripts

Im not sure what to really call them, but they are scripts that are loaded into the first scene and stay throughout the whole game. I am using them to store the player's progress through the whole game, games played, difficulty level etc.

They are created by adding 'DontDestroyOnLoad(gameObject);' in a script file that is created on the first scene. This means that when the scene changes, this object does not get removed from the game. One thing to note is that they are best created on a scene that the game never returns to. Otherwise, the game will create another copy each time that scene is reloaded.

The code that i am using to get other scripts to edit or read from the Global is:

private var VarName: ScriptName= null;

function Update ()
{

 if(VarName == null)
 {
   VarName= GameObject.Find("ObjectName").GetComponent(ScriptName);
 }


From my understanding,
'VarName' can be named anything so it is different.
'ScriptName' is the name of the script you are trying to access.
'ObjectName' is the name of the gameobject that the script is attached to.

Then to read or edit the scripts vars it is as simple as VarName.VarYourInterstedIn.


Because this script is loaded on the first scene and not in the current scene, i have to use this method of doings things as opposed to just dragging the gameobject into the inspector.



No comments:

Post a Comment