An question to ask...
How can I setup a variable that is belonged to many game objects?
EX: Once the variable value is changed in one object, all the other objects will be affected too.
Is it about global variables or defining classes? It confuses me.
Thanks for any help
Answer by syclamoth
Jan 31, 2012 at 12:57 PM
If you want a variable to be defined on all objects, define it static:
static var someVariable : int;
static int someVariable;
You can't edit static variables from the inspector, mind you, and they don't reset on scene-changes. It's generally a bad idea to use them- it would be better to create a single object which contains all of the global data, an provide references to it in every object that needs it.
var someObject : GlobalVariables;
You need a script called 'GlobalVariables' and you need to assign it in the inspector once you've created the object.
Ok,I am using global variable now. let's say there are two scripts in one scene,I have a static variable declared in script 1 , and I need to use that variable in script 2.
In script 2 , it reads the variable,no error occurs , but it's giving me Null value? Why is it like that?
Well, null values can happen if the value has never been assigned. Make sure that if the object is a reference type, it is guaranteed to always have an instance.
I did what it's told from this link: (at the bottom of the page)
Waaait, are they two instances of the same script, or are they two different scripts?
I have assigned the value in script 1 , but it just doesn't have any value in script 2. What do you mean by the object is a reference type?
Answer by Kryptos
Jan 31, 2012 at 01:11 PM
If you speak of changing the variable in the editor.
You might be looking for prefab: http://unity3d.com/support/documentation/Manual/Prefabs.html
You can create a script with a public variable along with its default value, then assign it to a gameObject. Make a prefab with this object. And all instances of this prefab will have this value by default.
If you want all objects to share the value at runtime, then a static variable is a solution.
Or you can create a manager (a singleton object) that keeps track of all your object, so that it can change all values at once.
Or you can create a singleton that holds this value and all of your other objects read this value at runtime.
Thanks for your response. I forget to mention that I want is that one variable can be shared between two scripts.
So does that mean I need to create a singleton that holds all the global variables and other scripts can get reference from it?
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a site navigation guide.
If you are a new user to Unity Answers, check out our FAQ for more information.
Make sure to check out our Knowledge Base for commonly asked Unity questions.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
Answers and Comments
7 People are following this question.
Communication between objects and other scripts, variables and properties
Rewind/Play animations on demand.
Can I show a numeric variable from the slider on the GUI?
How do I find the name and value for each variable of a script component, using Unity script?
variable = true from another script