Array not working properly
I'm trying to make a screen at the end of each level that displays the time it took to complete it and the record time it took to complete it. I created 2 scripts to make this work. Everything works perfectly when I use a single float to define the record time but when I change it to an array, I get 2 errors. I need it to be an array because I want the script to work with multiple levels so I need to store multiple records.
Unexpected node type. UnityEngine.JsonUtility:FromJson(String) End_Level:LoadRecordTime() (at Assets/Scripts/End_Level.cs:123) End_Level:OnTriggerEnter2D(Collider2D) (at Assets/Scripts/End_Level.cs:43)
IndexOutOfRangeException: Array index is out of range. End_Level.LoadRecordTime () (at Assets/Scripts/End_Level.cs:124) End_Level.OnTriggerEnter2D (UnityEngine.Collider2D other) (at Assets/Scripts/End_Level.cs:43)
Please help. Been trying to fix this for an hour but I can't find a way. I set int loadedLevel to 0 in the inspector.
Script 1:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;
public class End_Level : MonoBehaviour {
GameObject player;
public GameObject NewRecordText;
GameObject[] UI;
public GameObject EndScreen;
public Text LevelTime;
public Text RecordTime;
float time;
float bestTime;
LeaderboardInfo leaderboardInfo;
public int loadedLevel;
void Start () {
player = GameObject.FindGameObjectWithTag("Player");
leaderboardInfo = new LeaderboardInfo();
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
{
Time.timeScale = 0;
player.SetActive(false);
UI = GameObject.FindGameObjectsWithTag("UI");
for (int i = 0; i < UI.Length; i++)
{
UI[i].SetActive(false);
}
time = Timer.t;
LoadRecordTime();
SetTime();
SetRecordTime();
EndScreen.SetActive(true);
}
}
public void SetTime()
{
string minutes = ((int)time / 60).ToString();
string seconds = (time % 60).ToString("f2");
if (minutes.Equals("0"))
{
LevelTime.text = "Temps: " + seconds;
}
else
{
if (seconds.Length == 4)
{
LevelTime.text = "Temps: " + minutes + ":0" + seconds;
}
else
{
LevelTime.text = "Temps: " + minutes + ":" + seconds;
}
}
}
public void SetRecordTime()
{
if(time < bestTime)
{
string minutes = ((int)time / 60).ToString();
string seconds = (time % 60).ToString("f2");
if (minutes.Equals("0"))
{
RecordTime.text = "Record: " + seconds;
}
else
{
if (seconds.Length == 4)
{
RecordTime.text = "Record: " + minutes + ":0" + seconds;
}
else
{
RecordTime.text = "Record: " + minutes + ":" + seconds;
}
}
SaveRecordTime();
NewRecordText.SetActive(true);
}
else
{
string minutes = ((int)bestTime / 60).ToString();
string seconds = (bestTime % 60).ToString("f2");
if (minutes.Equals("0"))
{
RecordTime.text = "Record: " + seconds;
}
else
{
if (seconds.Length == 4)
{
RecordTime.text = "Record: " + minutes + ":0" + seconds;
}
else
{
RecordTime.text = "Record: " + minutes + ":" + seconds;
}
}
NewRecordText.SetActive(false);
}
}
public void LoadRecordTime()
{
if (System.IO.File.Exists(Application.persistentDataPath + "/leaderboardinfo.json"))
{
leaderboardInfo = JsonUtility.FromJson<LeaderboardInfo>(File.ReadAllText(Application.persistentDataPath + "/leaderboardinfo.json"));
bestTime = leaderboardInfo.TimeLevel[loadedLevel];
}
else
{
bestTime = Mathf.Infinity;
}
}
public void SaveRecordTime()
{
leaderboardInfo.TimeLevel[loadedLevel] = time;
if (loadedLevel > leaderboardInfo.LevelsFinished)
{
leaderboardInfo.LevelsFinished = loadedLevel;
}
string jsonData = JsonUtility.ToJson(leaderboardInfo, false);
File.WriteAllText(Application.persistentDataPath + "/leaderboardinfo.json", jsonData);
}
}
Scrpit 2:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LeaderboardInfo
{
public int LevelsFinished;
public float[] TimeLevel = new float[1];
}
THANKS FOR YOUR HELP
Your answer
Follow this Question
Related Questions
How to work with Timestamp data in Unity 0 Answers
How can I record the time that has passed between mouse clicks? 1 Answer
How do I return a series of arrays from a SimpleJSON Object 1 Answer
Time delay enemy respawn 3 Answers
Delay in time 0 Answers