Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by PvTGreg · Sep 22, 2014 at 07:57 AM · lists

How to Use lists Properly

Hi i have read the documentation on lists and have tried this but it dosent work the way i would think id also like help on how to display points in the list

ok so when you complete the level it will add your time to the list can you see anything wrong?

using UnityEngine; using System.Collections; using System.Collections.Generic; public class NextLevel : MonoBehaviour { public int LevelName; public bool ResetTime; // Use this for initialization

     public List<int> iList = new List<int>();
     void OnTriggerEnter2D(Collider2D col) 
     {
     
         if (col.tag == "NextLevel")
         {
             Application.LoadLevel (LevelName); 
 
             if (ResetTime == true)
             {    
                 iList.Add(PlayerPrefs.GetInt("Time"));
                 PlayerPrefs.SetInt("Time", 0);
             }
         }
     }
 
 
 }
 


and i display the highscore in the mainmenu

using UnityEngine; using System.Collections;

public class HighScore : MonoBehaviour {

 NextLevel s1;
 
 IEnumerator Start()
 {
     s1 = GetComponent<NextLevel>();
     yield return new WaitForEndOfFrame();
     foreach(int i in s1.iList)
         Debug.Log (i); // not sure how to display it id like to use    
                                    // labels or something

                  
 }

}

Comment
Add comment · Show 1
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
avatar image PvTGreg · Sep 22, 2014 at 08:00 AM 0
Share

ill also add when it comes to displaying it i get this null refrence NullReferenceException: Object reference not set to an instance of an object HighScore+c__Iterator0.$$anonymous$$oveNext () (at Assets/Scripts/HighScore.cs:15)

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by GameVortex · Sep 22, 2014 at 08:21 AM

The NullReferenceException does not have anything to do with the list. The list stuff you are doing is fine. The error comes from the fact that your variable s1 does not have a value. You have to initialize the variable before using it. Either by dragging the NextLevel component into the variable in the Highscore inspector, or searching for it through code.

This here is a very good read on NullReferenceException errors, and should help you debug those errors yourself next time: NullReferenceException - What is it and why do i get it?

Comment
Add comment · Show 3 · Share
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
avatar image GameVortex · Sep 22, 2014 at 08:28 AM 0
Share

Sorry, i saw now that you are adding the value to s1 right before the yield. Is the NextLevel component on the same GameObject as the HighScore component?

avatar image PvTGreg · Sep 22, 2014 at 11:22 AM 0
Share

the highscore is in the main menu seperate scene form where the list is created in nextlevel will i have to combine theese?

avatar image PvTGreg · Sep 22, 2014 at 11:44 AM 0
Share

ok i added nextlevel to the main scene and the null refrence cleared and it is displaying 0 now how can i get the list to add to keep the time from the previous scene

avatar image
0

Answer by PvTGreg · Sep 22, 2014 at 02:06 PM

OK i know answers are not for comments but im not going to open a new question the same question if i put this as a comment it will be to big

Ok i have been following some tutorials and reading threads on lists all day and i have come to this this will allow me to save and load the previous time with ease but id like to know how to do multiple entries to the 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 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") });
 
         //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));
                                        
         }
     }
     
 }

Comment
Add comment · Share
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

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

25 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

Related Questions

Multiple Cars not working 1 Answer

UnityEngine.Input.GetMouseButton(1)) issue 1 Answer

I made a better shader how do i fix[add _Shadow Strength]help???>Sorry that im asking for to much 1 Answer

Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers

Health Help D: 2 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