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
0
Question by Danor · Sep 19, 2014 at 05:04 PM · c#highscore

highscore/display system for multiple levels

hey guys, i have a scene set up with guiTexts: Level 1: , Level 2:, ... i have a working save/load system using serialization but i don't exactly know how to save the scores per level and displaying them in the guiTexts which are in a different scene. i also don't know how to test if what i have so far is currently working right.

here is the saving script that saves at the end of the level and gets the score from an other script and saves it to highscores arraylist which then is saved to Highscores arraylist in the GameControl script:

 using UnityEngine;
 using System.Collections;
 using System;
 using System.Runtime.Serialization.Formatters.Binary;
 using System.IO;
 
 public class Saving : MonoBehaviour
 {
     ArrayList highscores = new ArrayList();
     ScoreManager Score;
     void Start(){
          Score = GameObject.Find("Score").GetComponent<ScoreManager>();
     }
 
 
     void OnTriggerEnter( Collider other)
     {
         if(other.gameObject.tag == "Player")
         {
             GameObject[] NoOrbs;
             NoOrbs = GameObject.FindGameObjectsWithTag("Pickup");
             int count = NoOrbs.Length;
             if(count == 0){
             GameControl.control.levelcount += 1; //add +1 to levelcount for continue/levelLoading
 
                 //here is the part of the code that saving the scores at the right position, i hope..
                 int newScore = (int)Score.score; //get score and put it in int in newScore
                 int rawlevelCount = Application.levelCount;
                 int dontCountMenus = 2;
                 int levelCount = rawlevelCount - dontCountMenus;
                 highscores.Insert(levelCount, newScore);
                 GameControl.control.Highscores = highscores; //save current highscores to GameControl Highscores
                 GameControl.control.Save(); //saveGame
 
             }

and the GameControl script that should serializes the Array:

 using UnityEngine;
 using System.Collections;
 using System;
 using System.Runtime.Serialization.Formatters.Binary;
 using System.IO;
 
 public class GameControl : MonoBehaviour {
     public static GameControl control;
 
     public int levelcount;
     public ArrayList Highscores = new ArrayList();
     void Awake () {
         if(control == null)
         {
             DontDestroyOnLoad(gameObject);
             control = this;
         }
         else if(control != this)
         {
             Destroy(gameObject);
         }
     }
     public void Save()
     {
         BinaryFormatter bf = new BinaryFormatter();
         FileStream file = File.Create(Application.persistentDataPath + "/playerInfo.dat");
         
         PlayerData data = new PlayerData();
         data.levelcount = levelcount;
         data.Highscores = Highscores;
 
         bf.Serialize(file, data);
         file.Close();
     }
     public void Load()
     {
         if(File.Exists(Application.persistentDataPath + "/playerInfo.dat"))
         {
             BinaryFormatter bf = new BinaryFormatter();
             FileStream file = File.Open(Application.persistentDataPath + "/playerInfo.dat", FileMode.Open);
             PlayerData data = (PlayerData)bf.Deserialize(file);
             file.Close();
 
             levelcount = data.levelcount;
             Highscores = data.Highscores;
         }
     }
     public void overwriteSaveFile()
     {
         if(File.Exists(Application.persistentDataPath + "/playerInfo.dat"))
         {
             BinaryFormatter bf = new BinaryFormatter();
             File.Delete(Application.persistentDataPath + "/playerInfo.dat");
         }
     }
 }
 [Serializable]
 class PlayerData
 {
     public int levelcount;
     public ArrayList Highscores;
 }
 

none of these scripts give errors.

now what i need to find out is: is this currently working and if it does how do i write the variables from the array to the correct guiText(called Level1score) "Level 1:" + score from array position 1; guiText(called Level2score) "Level 2:" + score from array position 2; etc until atleast 55 and leaving the ability to add another level( hence why i'm using arraylist instead of normal array)

thanks in advance

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
0

Answer by SnotE101 · Sep 19, 2014 at 07:18 PM

try to save the score to a .txt file and extract in the OnGui function when you need to access it in the start of the script.nthe txt file won't change through loading levels and you have a lot more memory allowed to be saved to this file type.

http://answers.unity3d.com/questions/54822/write-to-a-text-file-in-resrouce-folder.html Check this out to save variables in a text file.

Comment
Add comment · Show 2 · 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 Danor · Sep 19, 2014 at 09:18 PM 0
Share

won't players be able to go in the textfile and edit the scores this way? and its already being saved in binary format by the gamecontrol script or am i misunderstanding your answer?

avatar image Danor · Sep 19, 2014 at 10:21 PM 0
Share

i have updated my question, could you take another look at it? thank you

avatar image
0

Answer by pgomes · Sep 19, 2014 at 10:32 PM

Use PlayerPrefs to save the highscores. Create a key for each level (e.g. PlayerPrefs.SetInt("HighscoreLevel1");). To display it, you can either read it directly from the player preferences, or save it to a local variable and read it from there.

Similar question:

http://answers.unity3d.com/questions/596490/how-to-save-high-scores.html

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 Danor · Sep 19, 2014 at 10:53 PM 0
Share

thank you for taking the time to answer, i'm a beginner so i'm sorry if i got this wrong but i've been reading on google that i should avoid playerprefs because they store in the register and that the contents of playerprefs can be easily changed by the player and if i get my game to a multiplayer level i would set up rankings for the best players and i wouldn't want people to change their score to get higher into the ranking. either way i will look at the link you provided but now that i have mentioned this do you still think i should go for playerprefs ins$$anonymous$$d of finding a way to do it with my current save/load system thats using serialization and formatting it to binary?

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Runs fine in editor, crash on switch platform to iOS. 0 Answers

How to Move an object after it was reproduced by spawn 0 Answers

Mathf.pingpong 4 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