- Home /
How to Use lists Properly
Hi i have read the documentation on lists and have tried this but it dosent work the way i would think id also like help on how to display points in the list
ok so when you complete the level it will add your time to the list can you see anything wrong?
using UnityEngine; using System.Collections; using System.Collections.Generic; public class NextLevel : MonoBehaviour { public int LevelName; public bool ResetTime; // Use this for initialization
public List<int> iList = new List<int>();
void OnTriggerEnter2D(Collider2D col)
{
if (col.tag == "NextLevel")
{
Application.LoadLevel (LevelName);
if (ResetTime == true)
{
iList.Add(PlayerPrefs.GetInt("Time"));
PlayerPrefs.SetInt("Time", 0);
}
}
}
}
and i display the highscore in the mainmenu
using UnityEngine; using System.Collections;
public class HighScore : MonoBehaviour {
NextLevel s1;
IEnumerator Start()
{
s1 = GetComponent<NextLevel>();
yield return new WaitForEndOfFrame();
foreach(int i in s1.iList)
Debug.Log (i); // not sure how to display it id like to use
// labels or something
}
}
ill also add when it comes to displaying it i get this null refrence NullReferenceException: Object reference not set to an instance of an object HighScore+c__Iterator0.$$anonymous$$oveNext () (at Assets/Scripts/HighScore.cs:15)
Answer by GameVortex · Sep 22, 2014 at 08:21 AM
The NullReferenceException does not have anything to do with the list. The list stuff you are doing is fine. The error comes from the fact that your variable s1 does not have a value. You have to initialize the variable before using it. Either by dragging the NextLevel component into the variable in the Highscore inspector, or searching for it through code.
This here is a very good read on NullReferenceException errors, and should help you debug those errors yourself next time: NullReferenceException - What is it and why do i get it?
Sorry, i saw now that you are adding the value to s1 right before the yield. Is the NextLevel component on the same GameObject as the HighScore component?
the highscore is in the main menu seperate scene form where the list is created in nextlevel will i have to combine theese?
ok i added nextlevel to the main scene and the null refrence cleared and it is displaying 0 now how can i get the list to add to keep the time from the previous scene
Answer by PvTGreg · Sep 22, 2014 at 02:06 PM
OK i know answers are not for comments but im not going to open a new question the same question if i put this as a comment it will be to big
Ok i have been following some tutorials and reading threads on lists all day and i have come to this this will allow me to save and load the previous time with ease but id like to know how to do multiple entries to the list
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using System.Collections;
//You must include these namespaces
//to use BinaryFormatter
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
public class HighScores : MonoBehaviour {
//High score entry
public class TimeEntry
{
//Players name
public string name;
//Score
public int time;
}
//High score table
public List<TimeEntry> Times = new List<TimeEntry>();
void SaveTimes()
{
//Get a binary formatter
var b = new BinaryFormatter();
//Create an in memory stream
var m = new MemoryStream();
//Save the scores
b.Serialize(m, Times);
//Add it to player prefs
PlayerPrefs.SetString("Times",Convert.ToBase64String(m.GetBuffer()) );
}
void Start()
{
Times.Add(new TimeEntry { name = "Time", time = PlayerPrefs.GetInt("Time") });
//Get the data
var data = PlayerPrefs.GetString("Times");
//If not blank then load it
if(!string.IsNullOrEmpty(data))
{
//Binary formatter for loading back
var b = new BinaryFormatter();
//Create a memory stream with the data
var m = new MemoryStream(Convert.FromBase64String(data));
//Load back the scores
Times = (List<TimeEntry>)b.Deserialize(m);
}
}
void OnGUI()
{
foreach(var time in Times)
{
GUILayout.Label(string.Format("{0} : {1:#,0}", time.name, time.time));
}
}
}
Your answer

Follow this Question
Related Questions
Multiple Cars not working 1 Answer
UnityEngine.Input.GetMouseButton(1)) issue 1 Answer
I made a better shader how do i fix[add _Shadow Strength]help???>Sorry that im asking for to much 1 Answer
Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers
Health Help D: 2 Answers