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 ronronmx · Feb 26, 2013 at 06:18 AM · savesavinggamecontroller

Saving player stats for each level, static class or not ?

I'm creating a LevelStats class that I will use to save information about each level, for example, the best time, the number of chests collected, number of stars won etc...

But I am not sure what the best way to build it is, as a static class with static functions, or as a normal class which I create new instances of for each level? The way I am saving values is: ProfileName.LevelName.StatName

If static, then I could save any stat from anywhere with:

LevelStats.SaveLevelBestLap( Application.loadedLevelName, laptime );

if not static, then I would need to create an instance of it in any class that will need to save stats:

public LevelStats levelStats = new LevelStats( Application.loadedLevelName );

levelStats.SaveLevelBestLap( laptime );

I'm looking for a few tips from you guys with experience with this type of stuff. Is using a static class a better fit?

Thanks guys!

Stephane

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

Answer by Tarlius · Feb 28, 2013 at 08:33 AM

I think you want to google "Singleton" and have a look at the PlayerPrefs class.

A singleton setup basically looks something like this:

 public class SaveData : MonoBehaviour {
     // This is the singleton bit
     SaveData instance = null;
     public SaveData Instance {
         get {return instance;}
     }
 
     void Awake() {
         if(instance != null) Debug.LogWarning("There is more than one instance of SaveData in the scene!!");
         instance = this;
     }
 
     public void DoSomething() {}
 }

Then call with: SaveData.Instance.DoSomething();

You could also add a static wrapper method to the class like:

public static void DoSomethingStaticVersion() { Instance.DoSomething(); } // Note the name can't be the same

Or make a shortcut class and put all the statics in there

 public static class SD {
     public static DoSomething() { SaveData.DoSomething(); } // Different class, so can share name
 }

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 ronronmx · Feb 28, 2013 at 04:16 PM 0
Share

Hi Tarlius, and thank you for your time. I didn't want to use a Singleton because in theory, each level has its own LevelStats, so I was thinking about creating a new instance of the LevelStats() class for each level...but now that I think about it, there will never be 2 instances of LevelStats per level, so I guess a singleton would make sense...

$$anonymous$$y reasoning behind making it a static class is that I need to save different stats from a few different classes, and I don't want to create a new LevelStats() instance within each script that needs to save stats data, so making it a static class makes it easier to save stats data from any script. I do like your suggestion about making a shortcut class though, interesting idea!

And yes I am using PlayerPrefs to save the data I need.

Thanks, Stephane

avatar image Tarlius · Feb 28, 2013 at 11:32 PM 0
Share

You could build the key using the name of the level, perhaps?

Level1.isFinished Level2.isFinished Level3.checkpoint

avatar image ronronmx · Mar 08, 2013 at 05:29 AM 0
Share

Yes I am building the key using the name of the level, just like you mentioned. I ended up making the class static, that way I can easily save stats through static functions that accept the level name + stat name.

Thanks for your help

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

10 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

Related Questions

Saving problem unity project 5 Answers

Deliberately Forcing the Game to Crash 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Can I transfer the PlayerPrefs.plist file to the iPhone? 1 Answer

Save scene 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