Ok so i have a list in my highscore script and then when you complete the game the current time is added to the list well thats what is supposed to happen at the moment it just spits an error at me and i have no idea why
here is my main highscore script which holds 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 TimeEntry
{
//Players name
public string name;
//Score
public int time;
}
public class HighScores : MonoBehaviour {
//High score entry
public bool ShowTimes;
//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()
{
//if (ShowTimes == true)
//{
foreach(var time in Times)
{
GUILayout.Label(string.Format("{0} : {1:#,0}", time.name, time.time));
}
//}
}
}
and this is the script which adds the entry
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class NextLevel : MonoBehaviour {
public int LevelName;
public bool SetTime;
HighScores times;
void Start()
{
times = GetComponent<HighScores>();
}
void OnTriggerEnter2D(Collider2D col)
{
if (col.tag == "NextLevel")
{
if (SetTime == true)
{
times.Times.Add(new TimeEntry { name = "Time", time = PlayerPrefs.GetInt("Time") });
}
Application.LoadLevel (LevelName);
}
}
}