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
Your answer
Follow this Question
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