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 /
avatar image
0
Question by FlorianBaptiste · Dec 05, 2015 at 06:15 PM · listssave dataload level

HELP - Save and Load Level data in a list

Hello everybody,

I have been struggling with this issue for several days now so I am coming for help. Forgive me if my question might be simple but I am new with coding.

What I want to do is pretty simple. I would like to save all my levels data in a list. For now, I have 2 levels and each level has 3 variables: a string, an int and a bool. Each level is a different scene.

I also would like my game to be able to easily access the information (meaning the 3 variables).

Here is my code

 using UnityEngine;
 using UnityEngine.SceneManagement;
 using System.Collections;
 using System.Collections.Generic;
 using System;
 using System.Runtime.Serialization.Formatters.Binary; 
 using System.IO;
 
 public class CurrentLevelInfo : MonoBehaviour
 {
     public List <Level> savedLevels = new List<Level>();
 
     public static CurrentLevelInfo levelManager;
 
     // test
     string levelName;
 
 
     void Awake ()
     {
         LoadLevels ();
 
         // We load the name of the level
         levelName = Application.loadedLevelName;
 
         // And we ask the information about the specific level and we get his
         switch(levelName)
         {
         case "Level1":
     
             Level.current = savedLevels[0];
             break;
         case "Level2":
             Level.current = savedLevels[1];
             break;
         }
     }
         
     // Subscribe to event
     void OnEnable ()
     {
         NpcScript.HasFoundConstellation += NewConstellationFound;
 
     }
 
     // Unsubscribe to event
     void OnDisable ()
     {
         NpcScript.HasFoundConstellation -= NewConstellationFound;
     }
 
     void NewConstellationFound ()
     {
         Level.current.hasDiscoveredConstellation = true;
         SaveLevels ();
     }
 
     public void SaveLevels()
     {
     BinaryFormatter bf2 = new BinaryFormatter();
         FileStream file = File.Create(Application.dataPath + "/savedLevels.dat");
 
         bf2.Serialize(file, savedLevels);
         file.Close();
     }
 
     public void LoadLevels ()
     {
         if(File.Exists(Application.persistentDataPath + "/savedLevels.dat"))
         {
             BinaryFormatter bf2 = new BinaryFormatter();
             FileStream file = File.Open(Application.persistentDataPath + "/savedLevels.dat", FileMode.Open);
             savedLevels = (List<Level>)bf2.Deserialize(file);
             file.Close();
         }
     }
 }
 
 [Serializable]
 public class Level
 {
     public static Level current;
 
     public int neededConstellationsStars;
     public string constellationName;
     public bool hasDiscoveredConstellation;
 }

If you guys could help me. I can read the list info per level but saving and loading don't seem to work.

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 dasani2406 · Sep 01, 2017 at 07:32 PM 0
Share

I ma pretty new also, and I think you did not do the save and load code properly. I can see the public class level you written but you do not mention it in the save and load codes. I think that is the problem. if you found your answer can you post it. it would be a lot of help of me and other people. Thank you.

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by TheSOULDev · Sep 01, 2017 at 10:58 PM

You need to change the paths of the Save and Load functions to be the same - at this point, one is saving in your game folder, while the other is loading from the persistentData path, which is completely different. You're saving in one location, but trying to read the save from a completely different location in which it, of course, doesn't exist.

Also, use the using keyword when declaring file streams, so all the garbage is disposed of correctly.

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

6 People are following this question.

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

Related Questions

Como fazer download de arquivos enquanto o jogo funciona 0 Answers

How to save list of Sprites? 1 Answer

Retain values of a List of list of a class in editor? 1 Answer

How to save progress like high scores and money on android onto the device and online 1 Answer

Saving Data for a Class 1 Answer


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