- Home /
lists and player prefs adding more than one entry
Hi im looking for a way to add an entry to the list when the player finishes the game i cant get head around it can i have some help please here is the main script for creating and storing the list in player prefs ive tried using get component but due to it needind classes ect in the main highscores script it wont work
i use this currently
Times.Add(new TimeEntry { name = "Time", time = PlayerPrefs.GetInt("Time") });
to set the times but it is only one entry id like to use something like this to set the times when the player finishes the level
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") }); // this is just for a test i would like this in the next level script
//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));
}
}
}
and this is the next level script where the name and time will be set
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class NextLevel : MonoBehaviour {
public int LevelName;
public bool SetTime;
// Use this for initialization
void OnTriggerEnter2D(Collider2D col)
{
if (col.tag == "NextLevel")
{
if (SetTime == true)
{
}
Application.LoadLevel (LevelName);
}
}
}
Answer by Louis Watson · Sep 24, 2014 at 08:10 AM
either use GameObject.Find("HighScores") to get reference to your script or make the "Times" List static.
tried that but since adding a new entry requires the class timeentry it dosent allow me to use it
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class NextLevel : $$anonymous$$onoBehaviour {
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);
}
}
}
ok so how do i call the class now? it is already public so do i just have to acces the variables? o should i just recreate teh calss i this script
why would i do that? i do not need to use it in the next level script?
i have moved my TimeEntry class outside of the main class that allows me to acces it in side of next level now but when i complete the game this error apears
NullReferenceException: Object reference not set to an instance of an object
NextLevel.OnTriggerEnter2D (UnityEngine.Collider2D col) (at Assets/Scripts/NextLevel.cs:26)
Answer by Louis Watson · Sep 24, 2014 at 08:16 AM
then in addition to making the List static create a public static function inside your HighScores class that takes what you need to make a TimeEntry object as paramaters and call that from the other script. you could also call save in that function if you wished.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
A node in a childnode? 1 Answer
How to add elements from a List of another class to another List (C#) 1 Answer
How i can PlayerPrefs List<>? 0 Answers
Save and Load Function 2 Answers