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 /
This question was closed Jan 22, 2019 at 07:27 PM by JaykieX for the following reason:

i found the solution

avatar image
0
Question by JaykieX · Jan 23, 2019 at 07:05 AM · loadingsave dataspawning problems

Need Help with Save/Load Function

Hey, guys, I'm building Strategy/CityBuilding type of game and I'm trying to implement a saving system but saved items didn't instantiate I can't figure out what's problem I took this code from guide I read and change a little bit for my game but it didn't work here's code.

this is code on gameobject i want to save

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using System;

 
 public class EmptySpaceRegister : MonoBehaviour
 {
     public void Start()
     {
         GlobalControl.SaveEvent += SaveFunction;
     }
     public void OnDestroy()
     {
         GlobalControl.SaveEvent -= SaveFunction;
     }
     public void SaveFunction(object sender, EventArgs args)
     {
         EmptySpaceLocs emptyspace = new EmptySpaceLocs();
         emptyspace.PositionX = transform.position.x;
         emptyspace.PositionY = transform.position.y;
         emptyspace.PositionZ = transform.position.z;
 
         GlobalControl.Instance.GetListForScene().SavedEmptySpaces.Add(emptyspace);
     }
 }
 

this is GlobalController code that controls save system

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using System;
 using System.Runtime.Serialization.Formatters.Binary;
 using System.IO;
 using UnityEngine.SceneManagement;
 
 public class GlobalControl : MonoBehaviour
 {
     public static GlobalControl Instance;
 
     public List<AllBuildingLocs> BuildingList;
 
     public delegate void SaveDelegate(object sender, EventArgs args);
     public static event SaveDelegate SaveEvent;
 
 
     void Awake()
     {
         Application.targetFrameRate = 144;
 
         if (Instance == null)
         {
             DontDestroyOnLoad(gameObject);
             Instance = this;
         }
         else if (Instance != null)
         {
             Destroy(gameObject);
         }
 
     }
     public void InitializeSceneList()
     {
         if(BuildingList == null)
         {
             print("building list is null");
             BuildingList = new List<AllBuildingLocs>();
         }
         bool found = false;
 
         for(int i = 0; i < BuildingList.Count; i++)
         {
             if (BuildingList[i].SceneID == SceneManager.GetActiveScene().buildIndex)
             {
                 found = true;
                 print("Scene was found in saved lists!");
             }
         }
         if (!found)
         {
             AllBuildingLocs newList = new AllBuildingLocs(SceneManager.GetActiveScene().buildIndex);
             BuildingList.Add(newList);
 
             print("Created new list!");
         }
     }
     public AllBuildingLocs GetListForScene()
     {
         for (int i = 0; i < BuildingList.Count; i++)
         {
             if (BuildingList[i].SceneID == SceneManager.GetActiveScene().buildIndex)
                 return BuildingList[i];
         }
 
         print("Total list count: " + BuildingList.Count.ToString() + " , not found index: " + SceneManager.GetActiveScene().buildIndex.ToString());
         return null;
     }
 
     public CurrencyData LocalCopyOfCurrency;
     public bool IsSceneBeingLoaded = false;
     public bool IsSceneBeingTransitioned = false;
 
     public CurrencyManager currency;
     
     public void CurrencyColb()
     {
         LocalCopyOfCurrency.Gold = currency.GoldData;
         LocalCopyOfCurrency.Food = currency.FoodData;
         LocalCopyOfCurrency.Wood = currency.WoodData;
         LocalCopyOfCurrency.Stone = currency.StoneData;
     }
 
     public void FireSaveEvent()
     {
 
         GetListForScene().SavedEmptySpaces = new List<EmptySpaceLocs>();
        
         if (SaveEvent != null)
             SaveEvent(null, null);
     }
 
     public void SaveData()
     {
         if (!Directory.Exists("Saves"))
             Directory.CreateDirectory("Saves");
 
         FireSaveEvent();
 
         BinaryFormatter formatter = new BinaryFormatter();
         FileStream saveFile = File.Create("Saves/save.binary");
         FileStream SaveObjects = File.Create("saves/saveObjects.binary");
 
         CurrencyColb();
 
 
         formatter.Serialize(saveFile, LocalCopyOfCurrency);
         formatter.Serialize(SaveObjects, BuildingList);
 
         saveFile.Close();
         SaveObjects.Close();
 
         print("Saved!");
     }
     public void LoadData()
     {
         BinaryFormatter formatter = new BinaryFormatter();
         FileStream saveFile = File.Open("Saves/save.binary", FileMode.Open);
         FileStream saveObjects = File.Open("Saves/saveObjects.binary", FileMode.Open);
         
 
         LocalCopyOfCurrency = (CurrencyData)formatter.Deserialize(saveFile);
         BuildingList = (List<AllBuildingLocs>)formatter.Deserialize(saveObjects);
 
         saveFile.Close();
         saveObjects.Close();
 
         currency.GoldData = LocalCopyOfCurrency.Gold;
         currency.FoodData = LocalCopyOfCurrency.Food;
         currency.WoodData = LocalCopyOfCurrency.Wood;
         currency.StoneData = LocalCopyOfCurrency.Stone;
 
         print("Loaded");
     }
     public void SaveButton()
     {
         SaveData();
     }
     public void LoadButton()
     {
         LoadData();
     }
 }
 

and this is spawner code

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class MapManager : MonoBehaviour
 {
     public GameObject EmptySpacePrefab;
 
     void Start()
     {
         GlobalControl.Instance.InitializeSceneList();
 
         if (GlobalControl.Instance.IsSceneBeingLoaded || GlobalControl.Instance.IsSceneBeingTransitioned)
         {
             AllBuildingLocs localList = GlobalControl.Instance.GetListForScene();
 
             if (localList != null)
             {
                 print("Saved potions count: " + localList.SavedEmptySpaces.Count);
 
                 for (int i = 0; i < localList.SavedEmptySpaces.Count; i++)
                 {
                     GameObject EmptySpace = (GameObject)Instantiate(EmptySpacePrefab);
                     EmptySpace.transform.position = new Vector3(localList.SavedEmptySpaces[i].PositionX,
                                                                     localList.SavedEmptySpaces[i].PositionY,
                                                                     localList.SavedEmptySpaces[i].PositionZ);
                 }
 
             }
             else
                 print("Local List was null!");
         }
     }
 }
 

in case if you guys ask this is savedata code im using

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 [System.Serializable]
 public class CurrencyData
 {
     public float Gold;
     public float Food;
     public float Wood;
     public float Stone;
 }
 
 [System.Serializable]
 public class EmptySpaceLocs
 {
     public float PositionX, PositionY, PositionZ;
 }
 
 
 [System.Serializable]
 public class AllBuildingLocs
 {
     public int SceneID;
     public List<EmptySpaceLocs> SavedEmptySpaces;
 
     public AllBuildingLocs(int newSceneID)
     {
         this.SceneID = newSceneID;
         this.SavedEmptySpaces = new List<EmptySpaceLocs>();
     }
 }

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

  • Sort: 

Follow this Question

Answers Answers and Comments

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

Saveing and Loading Problem 0 Answers

Save and load player position in main scene 1 Answer

How to load a score form a saved game vs. the level just completed 0 Answers

Trouble Saving and Loading Serialization 0 Answers

I am trying to make a dat file decoder to decode my game data but I can't figure it out please help 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