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
8
Question by AtomicMarine · Jan 08, 2011 at 11:05 AM · variablechangestatic

Static Variables between Scenes

If I change a static variable in one scene will it remain changed when I load another scene?

for example if I use a script to change the variable

num from 1 to 2

then call on num in another scene will num be 1 or 2?

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
28
Best Answer

Answer by Statement · Jan 08, 2011 at 11:07 AM

Static variables remember their values across scenes. So, num will be 2 in the next scene in your example.

Example:

using UnityEngine; public class StaticExample : MonoBehaviour { public static int counter = 0; void Start() { Debug.Log(counter++); Invoke("ReloadLevel", 3); }

 void ReloadLevel()
 {
     Application.LoadLevel(Application.loadedLevel);
 }

}

0

UnityEngine.Debug:Log(Object)

StaticExample:Start() (at Assets/StaticExample.cs:7)

1

UnityEngine.Debug:Log(Object)

StaticExample:Start() (at Assets/StaticExample.cs:7)

2

UnityEngine.Debug:Log(Object)

StaticExample:Start() (at Assets/StaticExample.cs:7)

Comment
Add comment · Show 3 · 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 AtomicMarine · Jan 08, 2011 at 11:15 AM 0
Share

Awesome Answer! if I could vote it up twice I would :) very in depth and thanks man, I was planning on using static variables for getting stats between scenes and now that I see that it is possible I will be :)

avatar image Statement · Jan 08, 2011 at 11:31 AM 2
Share

In depth? :) I took the most shallow example I could think of (with added output so you dont need to run the program to see the results). There are many approaches for stats keeping in Unity Answers. Just search for them to see how people handle this issue. Ins$$anonymous$$d of using static variables you can also set some objects to not destroy on load.

avatar image Statement · Jan 08, 2011 at 11:31 AM 1
Share

That way they live across scenes.

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

No one has followed this question yet.

Related Questions

Change static variable in JS through Csharp 1 Answer

change variable name inside IF statement, or add text to it 3 Answers

An instance of type X is required to access non static member Y [javascript] 4 Answers

Static array variable 1 Answer

Public variable not showing in Inspector(Solved) 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