Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by miner3115 · Mar 18, 2018 at 10:51 PM · timearraysjsonrecord

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

76 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges