• 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
Question by leoj3n_ · May 15, 2012 at 08:44 AM · javascriptvariablefunctionglobal

How do you achieve variables and functions that are global between scenes? What is the BEST way?

(Static functions/variables) VS (a reference to a GameObject)

Should I use static functions? Or should I use a reference to a GameObject? Is there another better way all together?

If I have data and functions I want to be able to access and change/execute from anywhere (including in-between scenes), should I just make those static and do things like GameManager.numPlayers which lets me do this as well (from anywhere): GameManager.numPlayers = 1 Or should I reference an instance of a script attached to a GameObject as a component?

My understanding of the latter way is: a reference to a kind of "global GameObject" would need to be declared at the header of each script like this: private var globalGameObject : GameObject; and then defined in Start() like this: globalGameObject = FindObjectOfType( GlobalGameObject ); and then used in other functions (in the same script) like this: gameObject.GetComponent( GlobalGameObject ).numPlayers

Note: ...this "`GlobalGameObject.js`" script would have had to have been attached to a GameObject that had had DontDestroyOnLoad( transform.gameObject ); called upon it.

Therefore the static variables/functions seem to have more syntactic sugar. Especially because of the fact that there's no adding as a Component to an instantiated GameObject (that has had DonstDestroyOnLoad() called upon it). Static variables like GameManager.numPlayers seem to work and retain their values throughout gameplay. Is there any danger in using static variables defined in non-attached scripts like this?

Is there a better way all together?

How do you achieve variables and functions that are global between scenes?

Let me update this question: I forgot to mention that sometimes, even when using static variables/functions, the script is being attached to a "global GameObject" because some variables require setup in a for loop. The only way I know to achieve this for loop is by attaching as a Component to an instantiated GameObject.

Update 2: I am using JavaScript.

Comment
aldonaletto

People who like this

1 Show 0
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

1 Reply

  • Sort: 
avatar image
Best Answer

Answer by Kryptos · May 15, 2012 at 09:33 AM

Take a look at the singleton pattern. There are lots of topics in Unity Answers and Forums. For example:

  • Singletons with coroutines (Unity Answers)

  • Singleton (Unify Community)

  • Component-based singleton (Unity Answers)

I recommend using a component-based singleton:

  1. You can have several components on this persistent game object that deal with different parts of your game (game logic, players management, audio, etc.).

  2. These components (and more specifically MonoBehaviours) will receive Unity events such as OnEnable, OnLevelWasLoaded and OnApplicationQuit (and even Update which can be useful for managing the game logic).

Comment
Bunny83

People who like this

1 Show 0 · 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

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Lower player's health from separate script. 2 Answers

Variable Not Changing. 0 Answers

Global variables - static keyword ? UnityScript? javascript? 1 Answer

Javascript Movement Script. Why am I getting these errors? 2 Answers

Issues referencing variables between functions 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