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
}
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.
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));
}
}
}