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
2
Question by Sophia Isabella · Sep 26, 2012 at 06:42 AM · c#

How can I have a static class I can access from any place in my game?

If I have the following code:

 static class SceneData
 {
    static int width=100;
    static int score;
    static GameObject soundManager;
 }

Can I use this to keep score that's available in all my scenes? If so then do I have to make an empty gameobject in each scene and then just attach the script to that or is there more I have to do?

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

5 Replies

· Add your reply
  • Sort: 
avatar image
33

Answer by dscroggi · Sep 26, 2012 at 10:56 AM

Sure! I am using the same methods in my own project right now.

First let me say that a guy named Petey has a youtube channel called BurgZergArcade that has some really awesome Unity3d C# programming videos!

That channel is here: http://www.youtube.com/user/BurgZergArcade

Anyway what I usually do that I learned from Petey is make a new empty gameobject and name it GameMaster, then make a C# script named GameMaster.cs.

There's at least two ways to do this. Here's the first way:

 using UnityEngine;
 
 public class GameMaster : MonoBehaviour 
 {
     public static int score;
     
     void Awake()
     {
         DontDestroyOnLoad(this);
     }
 }

This is the quick and easy way to access variables like 'score' from any class. To do this just call GameMaster.score from anywhere. The problem with this method is that it only works well for simple data types like int. Most of the time we want to drag and drop in Assets via the inspector, and this isn't allowed with static variables.

In this case we create what's called a singleton. This is something Leepo from M2H uses in his multiplayer tutorials. We create a regular class and then make one single static instance of the whole class. This is the second way:

 using UnityEngine;
 
 public class GameMaster : MonoBehaviour 
 {
     public static GameMaster GM;
     
     public GameObject soundManager;
     public int score;
     
     void Awake()
     {
         if(GM != null)
             GameObject.Destroy(GM);
         else
             GM = this;
         
         DontDestroyOnLoad(this);
     }
 }

The beauty of using the singleton is that it can use the inspector to assign public assets and can still be used anywhere like a static class, even the functions will work from anywhere. We'd call the score for example by using GameMaster.GM.score

Of course there are many other ways to accomplish this, but this is how I like to do it. Hope that helps!

Comment
Add comment · Show 5 · 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 myfunkyside · Jul 26, 2014 at 11:45 PM 2
Share

You deserve more thumbs and at least one comment saying this is correct. Oh, and it should be marked as correct, obviously.

I learned this technique in an official Unity Tutorial Video: see this link, video @ 13.30...
so I'm pretty sure this is the right way to go.

I HAVE ONE THING TO ADD: he should make a prefab of that Game$$anonymous$$aster-object.

avatar image Railon23 · Feb 02, 2017 at 02:29 PM 0
Share

I also wanted to thank you for your answer and the link to that Youtube channel. I came across this thread looking for help regarding extension methods. It's funny that the youtuber had just created this video two days ago which explains it very well: https://www.youtube.com/watch?v=$$anonymous$$UckjRhEDsQ

avatar image luislodosm · Mar 25, 2017 at 02:56 PM 0
Share

Use OnValidate () ins$$anonymous$$d of Awake () if you need the singleton in the editor.

avatar image Lorrak · Jun 11, 2021 at 04:09 AM 0
Share

thank you, great explanation, and avoided the "make it look cool and unclear"

avatar image Aethenosity · Jun 04 at 10:35 PM 1
Share

Wouldn't it make more sense to reverse the Awake method? Like this:

 void Awake()
 {
     if(GM == null)
         GM = this;
     else
         GameObject.Destroy(this)
 }

That way you aren't deleting the exiting GameManager, and only deleting any that try to instantiate afterwards?

If not, thanks for any clarification

avatar image
3

Answer by SilentSin · Jul 27, 2014 at 02:03 AM

Yes you can do this, but you need to specify everything as public.

 public static class SceneData
 {
     public static int width = 100;
     public static int score;
     public static GameObject soundManager;
 }

Otherwise they default to private and your class will be useless.

Now you'll be able to use SceneData.width anywhere.

The other answers offer ways of having a class that inherits from MonoBehaviour which you can access from anywhere, but it doesn't look like you actually want any MonoBehaviour functionality, so a simple static class will work better.

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
avatar image
1

Answer by fafase · Sep 26, 2012 at 06:56 AM

The way I do it is that I have a normal variable (I call it temp) that I use in the scene and a static variable (static) that I use for the whole game.

The reason is, if you use the static var for your point and the guy loses, when he starts again, he still has the point from previous run.

You could use temp and if the guy wins the level then

 static += temp;

If the guy loses

 temp= 0;

Temp is lost when the new level is loaded, static remains. You can attach the static var to your player object. The static var will have a lifetime of the whole game and you can access it anywhere with ClassName.staticVar.

In case you would not know much about static yet, it has been subject for a lot of controversy as beginners tend to think that the easiness of access is a good feature. Beware of that.

http://answers.unity3d.com/questions/318376/static-non-static.html

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 Sophia Isabella · Sep 26, 2012 at 07:10 AM 0
Share

$$anonymous$$y game is a bit different in that it does not have a player object as such. Could I create a gameObject called StaticData in each scene and attach a static class to that. If I did that would it still have the same values from scene to scene?

avatar image fafase · Sep 26, 2012 at 07:17 AM 0
Share

The game Object would be a new one but the data would be the same ones. It would be a new object linked to the same data.

avatar image Sophia Isabella · Sep 26, 2012 at 10:06 AM 0
Share

@fafase - What about the script thing that dscroggi is suggesting. Have you needed that before?

avatar image
1

Answer by dscroggi · Sep 26, 2012 at 07:34 AM

Throw this utility script on any game object and it will not get destroyed when scenes change. Make sure that game object has your static data on it. Btw as far as I'm concerned, static data is faster to access than instanced data, so you're on the right track.

 using UnityEngine;
 
 public class KeepOnLoad : MonoBehaviour 
 {
     void Awake () {
         DontDestroyOnLoad(this);
     }
 }
 
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 Sophia Isabella · Sep 26, 2012 at 10:05 AM 0
Share

@dscroggi - Can you explain a bit more? It's a method called DontDestroyOnLoad(this)?

avatar image fafase · Sep 26, 2012 at 10:21 AM 0
Share

when you start a new scene, all objects from the previous scene are destroyed. This function tells to keep the variable when loading a new scene. Could be a solution. $$anonymous$$y issue is that if for some reason the object gets destroyed within the game, you would lose the data. The function keeps on load but I do not know if it preserves from Destroy. http://docs.unity3d.com/Documentation/ScriptReference/Object.DontDestroyOnLoad.html

avatar image Sophia Isabella · Sep 27, 2012 at 02:49 PM 0
Share

@dscroggi - Can you show me how the class would look that I would need to hold the static data. All I need to do is to hold one integer for score? Thanks

avatar image
1

Answer by Garrom · Mar 26, 2017 at 07:03 PM

just declerate public static class and keep it in asset folder. public static class may look like this

 public static class TheStaticClass 
     {
          public static int uselessNumber = 33
     }

you can acess uselessNumber from any place in any code by

 TheStaticClass.uselessNumber



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

19 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Initialising List array for use in a custom Editor 1 Answer

Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer

Flip over an object (smooth transition) 3 Answers


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