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 3dDude · Jun 06, 2010 at 12:57 PM · staticvar

getting static vars from other scenes?

hi i was just wondering if it is possible to access static vars from other scripts in other scenes? because i am making a game where you can buy weapons and then use them in different levels but all of that is in different scenes so i dont know how to access the money vars in the different scenes. at first i tried DontDestroyOnLoad()
but that didn't work because when you went back to the first scene it made two versions of the object.

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
3

Answer by Murcho · Jun 06, 2010 at 02:30 PM

Static variables only get created once across all objects, so even though you have 2 instances of the same object, the static variable declared inside that object is shared between both instances of the object. E.G.

// This is our class
public class SomeClass : MonoBehaviour
{
    // This is our static variable
    public static float someFloat = 10.0f;
}

someFloat will be the same no matter where you access it from E.G.

// Another class
public class OtherClass : MonoBehaviour
{
    void Update()
    {
        // Print someFloat to the debug log
        Debug.Log(SomeClass.someFloat.ToString());
    }
}

So your instances of the class shouldn't change. Now whether Unity resets the static variable on loading another scene I'm not sure, but what you can do is set it up like a Singleton, by checking if there is another copy of the class in the scene like so.

// This is our class public class SomeClass : MonoBehaviour { // This is our static variable public static float someFloat = 10.0f; // Variable to store a reference to class private static SomeClass instance;

 void Awake()
 {
     if(instance == null)
     {
         instance = this;
     }
     else
     {
         Destroy(this);
     }
 }

}

What this does is stores a reference to itself inside a static variable that is shared across all instances of the class, and in Awake(), checks to see if there is already a copy of itself in the static variable. If there is, then it destroys itself, otherwise it places itself in there. By using DontDestroyOnLoad() on this object, you will only have one copy in any scene you load, and will remain the same object throughout.

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 3dDude · Jun 07, 2010 at 01:35 AM 0
Share

could you write this code in javascript because i use java.

avatar image
1

Answer by Eric5h5 · Jun 06, 2010 at 02:29 PM

You can only access objects in the current scene. DontDestroyOnLoad is correct; in order to prevent more than one copy, you can either have it on a script in a scene that's executed once only (like a splash screen), or else detect in the Awake function if another copy already exists, in which case destroy itself.

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

No one has followed this question yet.

Related Questions

Should I create local copy of global variable? 3 Answers

Best way to share a variable 1 Answer

Make Score Go Up At a Collision 2 Answers

Error Assigning GameObject To Var Inside A Class 3 Answers

Accessing a js static var from a c# script 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