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 penguin21 · Oct 31, 2018 at 03:51 PM · serializationsavesave data

Help with serialization

I want to create a save file with the statistics of levels and levels unlocked but it does not leave me by line 96 I will pass the script and the saved class: Script:

 using System.Collections;
 using System.Collections.Generic;
 using System;
 using System.IO;
 using System.Runtime.Serialization.Formatters.Binary;
 using UnityEngine;
 
 public class SaveGame : MonoBehaviour {
 
     public string saveName = "file1.sav";
     public static Level[] levelsList;
     public Level[] tempList;
     public lvlPrefs[] lvlConf;
 
     // Use this for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void Update () {
         levelsList = FindObjectsOfType(typeof(Level)) as Level[];
         tempList = levelsList;
     }
 }
 
 public static class SaveLoad
 {
     public static List<SaveFile> savedGames = new List<SaveFile>();
     public static List<LevelSave> savedLevels = new List<LevelSave>();
 
     //it's static so we can call it from anywhere
     public static void Save(string fileName, MapPre statManager)
     {
         //SaveLoad.savedGames.Add(SaveFile.current);
         BinaryFormatter bf = new BinaryFormatter();
         //Application.persistentDataPath is a string, so if you wanted you can put that into debug.log if you want to know where save games are located
         FileStream file = File.Create(Application.persistentDataPath + "/" + fileName); //you can call it anything you want
         SaveFile data = new SaveFile(statManager);
         bf.Serialize(file, data);
         file.Close();
         Debug.Log("Saved");
     }
 
     public static int[] Load(string fileName)
     {
         if (File.Exists(Application.persistentDataPath + "/" + fileName))
         {
             BinaryFormatter bf = new BinaryFormatter();
             FileStream file = new FileStream(Application.persistentDataPath + "/" + fileName, FileMode.Open);
             SaveFile data = bf.Deserialize(file) as SaveFile;
             file.Close();
             MapPre.tempMarioPos.x = data.mapPositionX;
             MapPre.tempMarioPos.y = data.mapPositionY;
             MapPre.tempMarioPos.z = data.mapPositionZ;
             //List<Level> levels = (List<Level>)bf.Deserialize(file);
             return data.stats;
             
         }
         else
         {
             Debug.LogError("File does not exist sorry :(.");
             return new int[4];
         }
     }
 }
 
 [Serializable]
 public class SaveFile
 {
     public static SaveFile current;
     public int[] stats;
     public float mapPositionX, mapPositionY, mapPositionZ;
     public Level[] levelForSave;
     public LevelSave[] levelData;
     public lvlPrefs[] lvlC;
     //public List<Level> levelList = new List<Level>();
 
     public SaveFile(MapPre player){
         stats = new int[5];
         stats[0] = player.coinCurrent;
         stats[1] = player.scoreCurrent;
         stats[2] = player.currentItemBox;
         stats[3] = player.currentLives;
         stats[4] = player.currentPowerUp;
         mapPositionX = player.marioPos.x;
         mapPositionY = player.marioPos.y;
         mapPositionZ = player.marioPos.z;
         levelForSave = new Level[SaveGame.levelsList.Length];
         lvlC = new lvlPrefs[SaveGame.levelsList.Length];
         for (int i = 0; i < SaveGame.levelsList.Length; i++)
         {
             
             levelForSave[i] = SaveGame.levelsList[i];
             Debug.Log(levelForSave[i].gameObject.name);
             lvlC[i].lastCamName = SaveGame.levelsList[i].lastCameraName;
             //levelData[i].lastCamName = levelForSave[i].lastCameraName;
         }
         //for(i)
     }
 }
 

Class:

 [Serializable]
 public class lvlPrefs
 {
     public GameObject obj;
     public string sceneName, lastCheckpointName, lastCamName;
     public bool StarCoin1, StarCoin2, StarCoin3, levelComplete;
 }

Error code:

 NullReferenceException: Object reference not set to an instance of an object
 SaveFile..ctor (.MapPre player) (at Assets/Scripts/SaveGame.cs:96)
 SaveLoad.Save (System.String fileName, .MapPre statManager) (at Assets/Scripts/SaveGame.cs:39)
 MapPre.saveData () (at Assets/Scripts/MapWorld/MapPre.cs:53)
 UnityEngine.Events.InvokableCall.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:166)
 UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:58)
 UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:36)
 UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:45)
 UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:50)
 UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:261)
 UnityEngine.EventSystems.EventSystem:Update()
 

help me pls

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

101 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 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

Error when loading saved date : SerializationException: End of Stream encountered before parsing was completed. 2 Answers

Save multiple scriptableObjects in just one binary file 2 Answers

im getting a serialization exception eror in my load method 0 Answers

Save system for a Motherload-like game. 1 Answer

Error while saving game: SerializationException: Type 'UnityEngine.GameObject' in Assembly 'UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable. 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