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 23, 2014 at 11:43 AM · listplayerprefs

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); 
         }
     }
 
 
 }
 


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

2 Replies

· Add your reply
  • Sort: 
avatar image
-1
Best Answer

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.

Comment
Add comment · Show 13 · 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 PvTGreg · Sep 24, 2014 at 08:13 AM 0
Share

tried that but since adding a new entry requires the class timeentry it dosent allow me to use it

avatar image PvTGreg · Sep 24, 2014 at 08:19 AM 0
Share
 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

avatar image Louis Watson · Sep 24, 2014 at 08:25 AM 0
Share

make SaveTimes public static

avatar image PvTGreg · Sep 24, 2014 at 08:30 AM 0
Share

why would i do that? i do not need to use it in the next level script?

avatar image PvTGreg · Sep 24, 2014 at 08:33 AM 0
Share

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)
 
Show more comments
avatar image
0

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.

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

27 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

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


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