In my game I save the players best times through levels in PlayerPrefs. The problem is that it can’t seem to save those floats. It’s probably some stupid error on my part. I saved it using a for loop but it is not working. The value of the 1st value, is always zero what is the problem? here’s my code:
using Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Collections.Generic;
using System;
using System.Linq;
public class GameWideScript : MonoBehaviour {
public int ReachedLevel;
public int thislevel;
public float thisleveltime;
public float[] levelTimes;
public bool paused;
public int maxlevel;
float i = 1;
public void Start(){
levelTimes = new float[maxlevel + 1];
DontDestroyOnLoad (this.gameObject);
if(PlayerPrefs.HasKey("Level " + maxlevel)){
Debug.Log("restoring data");
ReachedLevel = PlayerPrefs.GetInt ("reachedlevel");
foreach(float h in levelTimes)
{
h.Equals(PlayerPrefs.GetFloat("Level " + i));
}
if(levelTimes[maxlevel] == PlayerPrefs.GetFloat ("Level " + maxlevel)){
print ("data restoration successful");
Debug.Log("data restored");
}
}
else{
print("inititating data");
PlayerPrefs.SetInt ("reachedlevel", ReachedLevel);
for(int i = 1; i <= maxlevel; i++)
{
if(!PlayerPrefs.HasKey("Level " + i)){
PlayerPrefs.SetFloat("Level " + i, 0);
}
}
}
print (ReachedLevel);
}
//public int SetVariable
IEnumerator Wait(float wait) {
yield return new WaitForSeconds(wait);
Application.LoadLevel ("GameOverScreen");
}
public void GameOver(){
StartCoroutine (Wait(1f));
}
public float[] GetTimes(){
return levelTimes;
}
public void SetTimes(float[] times){
levelTimes = times;
}
public void Save(){
PlayerPrefs.SetInt ("reachedlevel", ReachedLevel);
for(int i = 1; i <= maxlevel; i++)
{
PlayerPrefs.SetFloat("Level " + i, levelTimes*);*
-
}*
PlayerPrefs.Save();
-
}*
-
public void Delete(){*
-
PlayerPrefs.DeleteAll ();*
-
}*
I don’t know why it’s not working! Thanks for any help guys!