Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
1
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
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
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
Add comment · 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

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.

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

What is this C# code in javascript 0 Answers

How to call a variable that was defined in FixedUpdate in javascript 1 Answer

variable = true from another script 2 Answers

passing a javascript array into a function sorts it? 1 Answer

function CheckPassword():boolean{ is void? 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