- Home /
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
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
Answer by raulrsd · Mar 15, 2015 at 07:43 PM
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? $$anonymous$$ine is not (can not be static. its attached to different units).
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.
You don't have to make a static class to have a static variable. If a static variable is associated with a particular class, I$$anonymous$$O it makes more sense to make it a member of that class. So in your case a quick way to do what you're after is just to make PathLength a public static member of PathInput.
But is that really what you're after? The example you give suggests that you don't actually want PathLength to be global or static, you just need to be able to access it from a different class. In that case you may be better off following $$anonymous$$agicZelda's advice and access PathLength through a reference to the PathInput you're concerned with.
BTW your "does not contain a definition for pathLength" may simply be to do with the first letter being capitalised or not.
Answer by UNZoOM · Mar 20, 2015 at 06:29 PM
// 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.
Answer by Invictum · Mar 20, 2015 at 11:27 PM
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 $$anonymous$$SDN 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 $$anonymous$$onoBehavior, because I didn't want to have to attach it to a GO
Your answer
Follow this Question
Related Questions
Why can't I drag text into public text in the inspector when instantiated? 1 Answer
Andoid enter text without gui 1 Answer
Mirrored text for mirror projection 0 Answers
How to save script component variables at runtime from IOS device? 1 Answer
Is it possible to italicise GUI text without having italic font? 1 Answer