Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by shikan1997 · May 26, 2021 at 09:53 AM · scripting beginnerlearning

I wonder if we can create a List can remain even when we change scene or reopen game?

I'm learning Unity by creating a simple game, it's pretty done, but I want to create a Rank List that can contain top5 bestscore that Player ever had at an object that can't be destroyed when i'm change game scene, each time I push the "add" button, It'll check and save as a ranktop, I tried like this:

 public class BestScorestore : MonoBehaviour
 {
     public static BestScorestore Main;
     public List<Ranking> rankstorage;
     private const string bestScore = "Best Score";
     private const string Playername = "Player's name";
     public Ranking ranktop;
     private void Awake()
     {
         Singleconnect();
         isFirstrun();
     }
     void isFirstrun()
     {
         if (!PlayerPrefs.HasKey("isFirstrun"))
         {
             PlayerPrefs.SetInt(bestScore, 0);
             PlayerPrefs.SetInt("isFirstrun", 0);
             rankstorage = new List<Ranking>();
         }
     }
 void Singleconnect()
     {
         if (Main != null)
         {
             Destroy(gameObject);
         }
         else
         {
             Main = this;
             DontDestroyOnLoad(gameObject);
         }
     }
 public void checkRank(string nametop,int scoretop)
     {
         ranktop = new Ranking(nametop, scoretop);
         if (rankstorage.Count > 0)
         {
             for (int i = 0; i < rankstorage.Count; i++)
             {
                 if (ranktop.rankscore > rankstorage[i].rankscore)
                 {
                     rankstorage.Insert(i, ranktop);
                 }
             }
             if (rankstorage.Count > 5)
             {
                 for (int k = rankstorage.Count - 1; k > 4; k--)
                 {
                     rankstorage.Remove(rankstorage[k]);
                 }
             }
         }
         else{
             rankstorage.Add(ranktop);
         }
     }
 public void setPlayerName(string name)
     {
         PlayerPrefs.SetString(Playername, name);
     }
     public string getPlayerName()
     {
         return PlayerPrefs.GetString(Playername);
     }
 public void setBestScore(int score)
     {
         PlayerPrefs.SetInt(bestScore, score);
     }
     public int getBestScore()
     {
         return PlayerPrefs.GetInt(bestScore);
     }
 }
 public class Ranking
 {
     public string rankname;
     public int rankscore;
     public Ranking(string name, int score)
     {
         rankname = name;
         rankscore = score;
     }
 }

and in the GameManage's script i used like this one

 {
     [SerializeField]
     private List<Text> topname=new List<Text>();
     [SerializeField]
     private List<Text> topscore=new List<Text>();
     [SerializeField]
     private Text currentName, currentScore;
     private Ranking currentplayer;
     private string currentname;
     private int currentscore;
     void ShowRankList()
     {
         foreach (Text child in topname)
     {
         child.text = "";
     }
     foreach (Text child in topscore)
     {
         child.text = "";
     }
     currentname = BestScorestore.Main.getPlayerName();
     currentscore = BestScorestore.Main.getBestScore();
     currentName.text = "" + currentname;
     currentScore.text = "" + currentscore;
     var m = BestScorestore.Main;
     if (m.rankstorage == null) return;
     for (int i = 0; i < m.rankstorage.Count - 1; i++)
     {
         topname[i].text = m.rankstorage[i].rankname;
         topscore[i].text = "" + m.rankstorage[i].rankscore;
     }
     }
     public void addButton()
     {
         k.Clicksound();
         currentname = currentName.text;
     currentplayer = new Ranking(currentname, currentscore);
     BestScorestore.Main.setPlayerName(currentplayer.rankname);
     BestScorestore.Main.checkRank(currentplayer.rankname,currentplayer.rankscore);
     if (!BestScorestore.Main.rankstorage.Contains(currentplayer))
     {
         Debug.Log("Your score is too low to put in ranklist");
     }
     else Debug.Log("Add!");
     addScorePannel.SetActive(false);
     buttonPlus.GetComponent<Button>().enabled = true;
     foreach (GameObject child in k.closeButtons)
     {
         child.GetComponent<Button>().enabled = true;
     }
     ShowRankList();
     }
 }

but when i open Rank list, and push the "Add" button, it show:

 "NullReferenceException: Object reference not set to an instance of an object"

when i check rankstorage.count at my checkRank(string nametop,int scoretop)

Am I doing wrong somewhere? Please help!

I'm sorry that i wrote too much in there...

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

0 Replies

· Add your reply
  • Sort: 

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

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

Related Questions

Best way to learn how to utilize the Unity API? 2 Answers

Making a sword follow mouse movement 1 Answer

Possible Missing XML/DLL Files? 0 Answers

How to Script a 3D game like SOKOBAN GAME 0 Answers

How to make a TextMeshProUGUI shrink over time? 0 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