How do you make a global variable, global in Unity?

public float PathLength = 0f;
PathLength = PathLength + SegmentLength;

This gives me the correct data I want, displayed in the Inspector.
I want it displayed in the HUD.
So In My HUDScript I wrote this:

	mytext.text = "Line Length: " + PathLength;

I get this error:

error CS0103: The name `PathLength’ does not exist in the current context

It'd make more sense to reference the HUD text in the first script. So add using UnityEngine.UI; To the top of that script and have a public Text mytext in that script. Just drag the HUD Text object onto that public slot in the inspector.

if the script public float PathLength = 0f; PathLength = PathLength + SegmentLength; is in another script on another gameobject you will need to get a reference to that GameObject

3 Answers

3

When I want a really global variable what I do is to make a static class to store this variables, like this:

public static class GlobalVariables{
     public static float pathLength; 
}

This way every single script in my game could access this variable just writing:

GlobalVariables.pathLength = 1.0f;

GlobalVariables.pathLength += SegmentLength;

Or whatever.

mytext.text = "Line Length: " + PathInput.pathLength; ugg.... now I'm getting this: error CS0117: PathInput' does not contain a definition for pathLength' Is it a problem with the static class? Mine is not (can not be static. its attached to different units).

Tell more about class PathInput or post it's text.

can u show us PathInput class

if your public varialble is in a script with name for example globalScript.cs then write in the script other script where you want to use this: globalScript.PathLength And in globalScript.cs you have to write: public static float PathLength = 0f; PathLength = PathLength + SegmentLength; The globalScript class itself don't need to be static.

// MODIFY HUD SCRIPT

public var GiveMePathLengthGO: GameObject; // Drag the GO here which has the script attached to it which has the pathLength variable Initialized.

function Start()
{
 
}

function Update()
{
 // You can now use the pathLength like so,
mytext.text = "Line Length: "+GiveMePathLengthGO.GetComponent(PathLengthScript).pathLength ; 
}

Note : PathLengthScript is the script name where you have path length being initialized or wherever you would like to call it from.
This is a workaround , the most ideal way to use is Static class as raulrsd mentioned.

You actually should use a singleton if you really need a global.

http://wiki.unity3d.com/index.php/Toolbox

Toolbox is a slightly improved singleton for unity you can just grab the code for the classes and add, then make your global in toolbox cs. It’s just as easy, you can call Toolbox.Instance.Myglobals from anywhere. But it is a much better practice if you “need” a global or two.

While there is great debate as to not use singletons at all or not I think they all agree the old static poor practice. I still find singletons useful so I am of the lazy side of the argument I suppose.

@Invictum Hi I hope this thread is not too old. But the toolbox uses singletons and according to MSDN they are outdated. About the static classes: I did this with a bunch of variables, but they (apparently) get garbage collected at some point. It's the only explanation I can come up with, when I get the message value not assigned at random times. Is there a way to protect a certain class from garbage collection or is there another possible reason for this? BTW the static class was not a MonoBehavior, because I didn't want to have to attach it to a GO