Hi for some reason when i add an entry to my list when i load the main menu it dosent display can anyone see where i have gone wrong? i notice that it may be because its in the ontrigger enter but i dont see why it removes it
here is me adding info to the list
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class NextLevel : MonoBehaviour {
public int LevelName;
public bool SetTime;
HighScores times;
//TimeEntry entry;
void Start()
{
times = GameObject.Find("HighScore").GetComponent<HighScores>();
//entry = GetComponent<TimeEntry>();
}
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);
}
}
}
and this is the script that holds and saves my 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 {
public List<TimeEntry> Times = new List<TimeEntry>();
//High score entry
public bool ShowTimes;
public bool SaveToPlayerPrefs;
public bool SaveToFile;
//High score table
void SaveTimes()
{
if (SaveToFile == true)
{
//Get a binary formatter
var b = new BinaryFormatter();
//Create a file
var f = File.Create(Application.persistentDataPath + "/highscores.dat");
//Save the scores
b.Serialize(f, Times);
f.Close();
}
if (SaveToPlayerPrefs == true)
{
//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()
{
Debug.Log( Application.persistentDataPath );
if (SaveToFile == true)
{
//If not blank then load it
if(File.Exists(Application.persistentDataPath + "/highscores.dat"))
{
//Binary formatter for loading back
var b = new BinaryFormatter();
//Get the file
var f = File.Open(Application.persistentDataPath + "/highscores.dat", FileMode.Open);
//Load back the scores
Times = (List<TimeEntry>)b.Deserialize(f);
f.Close();
}
}
if (SaveToPlayerPrefs == true)
{
//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));
}
//}
}
}