• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by 071813g · Jan 31, 2012 at 12:46 PM · variable

Sharing variables value between game objects?

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by syclamoth · Jan 31, 2012 at 12:57 PM

If you want a variable to be defined on all objects, define it static:

JS:

 static var someVariable : int;

C#

 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.

Comment
Add comment · Show 9 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image 071813g · Feb 01, 2012 at 03:24 AM 0
Share

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?

avatar image syclamoth · Feb 01, 2012 at 03:26 AM 0
Share

Well, null values can happen if the value has never been assigned. $$anonymous$$ake sure that if the object is a reference type, it is guaranteed to always have an instance.

avatar image 071813g · Feb 01, 2012 at 03:31 AM 0
Share

I did what it's told from this link: (at the bottom of the page)

http://unity3d.com/support/documentation/ScriptReference/index.$$anonymous$$ember_Variables_26_Global_Variables.html

avatar image syclamoth · Feb 01, 2012 at 03:34 AM 0
Share

Waaait, are they two instances of the same script, or are they two different scripts?

avatar image 071813g · Feb 01, 2012 at 03:35 AM 0
Share

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?

Show more comments
avatar image
1

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image 071813g · Feb 01, 2012 at 03:29 AM 0
Share

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?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Instantiate only one prefab 3 Answers

Why is this method being called multiple times? 0 Answers

Pointer or Copied Value? 1 Answer

Only change a variable on the instaniated object not the prefab. 0 Answers

Unity auto unassigned skinned mesh render variables on Play 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges