- Home /
Global variables questions
The only way I know of to declare a global variable is using static, which would be okay but sometimes when I try to access those variables from another script Unity says I must create an instance of the variable to use it? Aside from that, is there any way to make a global variable that I can also input in the inspector?
If you want global variables without using static variables, consider having a singleton component, as described here -- http://answers.unity3d.com/questions/17916/singletons-with-coroutines.html
Answer by Peter G · Jul 21, 2011 at 02:06 AM
FALSE a static var is NOT a global var. public
makes a variable global, not static.
//This is perfectly legal
private static int myPrivateVar;
static makes a variable a class variable (its a little different than it is in C++) which means that it is associated with a class, not any of its instances. Public makes a variable or function accessible from outside functions. Hence the use of properties to encapsulate fields.
Unity will let you inspect any serializable public instance variable. Now that can sound intimidating if you aren't a programmer so let me break that down:
Serializable. All the Unity types, Mono objects, and the basic data types (int, float, string), and any classes you create that have the [Serializable] attribute.
public. The easiest one, they can't be private or protected.
Instance variable. As long as it isn't static, then its an instance variable.
The proper way to access a global variable (besides being through a property) is to get an instance of the class then read its value.
//C#
var instance = (MyClass)FindObjectOfType(typeof(MyClass));
Debug.Log(instance.variableName);
quote:Serializable. All the Unity types, $$anonymous$$ono objects, and the basic data types (int, float, string), and any classes you create that have the [Serializable] attribute.
I don't really understand what you mean,can you give me some c# script so that I may get it easily.Thanks
I actually made an oversight writing that so let me make a small correction. Not all $$anonymous$$ono objects are displayed in the inspector. Some are, but others such as $$anonymous$$ethodInfo
(just to pick a random one) are not.
I'll give an example of each:
//Unity Types;
Transform t;
Renderer r;
//All classes in the Unity Scripting reference. *Note you could probably find one that isn't but generally, all classes that you would want to put in the inspector are available*
//$$anonymous$$onoObjects
//such as System.Collections.Generic.List<T>();
//Basic data types:
int i;
float myFloat = 2.0f;
string myString = "";
char myChar = 'a';
//classes with [Serializable].
[Serializable]
class $$anonymous$$yClass {
int a;
int b;
}
//Unity will let you inspect this class. Try creating a variable of this type and Unity will make a little dropdown let with an array that shows all the public variables that also meet these requirements.
PETER G, thank you sooo much, I've been searching for a way to keep my sword from taking damage for the player and this is my solution, this code works so well!
Answer by UdevMike · Jul 21, 2011 at 01:51 AM
C# example: public static int myVar;
Access it by writing the name of the script followed by the variable name myScript.myVar = 5;
Also note that you can declare a class like "public static class $$anonymous$$yGlobalVariables { ... }" -- you cannot create an instance of a static class, so this is a good idea if you have a class whose only purpose is to hold static global variables.
Answer by Eric5h5 · Jul 21, 2011 at 02:04 AM
Don't use static unless you only want one instance of the variable. Making a variable public makes it global, static has nothing to do with it (private variables can be static). http://unity3d.com/support/documentation/ScriptReference/index.Accessing_Other_Game_Objects.html
Answer by cregox · Sep 04, 2013 at 12:19 AM
Use `Singleton`s!
They're **much** better than using static.
Grab the singleton script above and simply use it as such:
public class MyClass : MonoBehaviour {
void Awake () {
Debug.Log(Manager.Instance.myGlobalVar);
}
}
Manager.cs
public class Manager : Singleton<Manager> {
public string myGlobalVar = "whatever";
}
Works perfect, thanks a lot! Very helpful!
PS: That post made me additionally think of reworking my "Camera$$anonymous$$anager" class as singleton, so that there is only one globally available class that has the references to all Camera objects in the scene.
Answer by karl_ · Jul 21, 2011 at 02:05 AM
You can access local variables by creating an instance of the script. As an example, lets say you have someVariable located in scriptA, which is attached to gameObjectA. You want to access it from scriptB, which is attached to gameObjectB.
Within scriptB:
function Update()
{
// Find the gameobject that scriptA is attached to
var instanceGO : GameObject.Find("gameObjectB");
// Create an instance of the script
var instanceScript : scriptA = instanceGO.GetComponent("scriptA");
// Set someVariable to 0.
instanceScript.scriptA.someVariable = 0;
}
Don't use quotes with GetComponent; it's slower, and if you make a typo, you only find out at runtime ins$$anonymous$$d of when you compile the script.
I did not know that, good bit of info to have. Also, as a side note- I normally wouldn't put a GetComponent() call in an Update function. This was just a cursory example bit.