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 Betmans · Dec 03, 2016 at 05:23 AM · unity 5positionsceneruntimefile

Load data from file, if scene changed, fails to load location

Hey Unity people! I am in a bit of trouble here, I made a singleton, e.g. GameControl object with a script of the same name attached to it, which gathers information from other objects and saves it to a file:

 using UnityEngine;
 using System.Collections;
 using System;
 using System.Runtime.Serialization.Formatters.Binary;
 using System.IO;
 using UnityEngine.SceneManagement;
 
 public class GameControl : MonoBehaviour {
 
     public Color color;
 
     public static GameControl control;
 
     //PLAYER STATS
     public float playerHP, Combatxp, playerPosX, playerPosY, playerPosZ;
     public int sceneIndex;
     //PLAYER STATS
 
     //ENEMY STATS
     public float currentHealth, myPosX, myPosY, myPosZ;
     //ENEMY STATS
 
     public bool gameSaved, gameLoaded;
     public float gameSavedF = 100;
 
     public GameObject player, enemy;
 
     void Start(){
 
         color = Color.white;
     }
 
     void Update(){
 
         player = GameObject.FindGameObjectWithTag ("Player");
         enemy = GameObject.FindGameObjectWithTag ("Enemy");
 
 
         if (Input.GetKeyDown (KeyCode.F5)) {
             Save ();
             player.GetComponent<Inventory> ().SaveInventory ();
         }
         if (Input.GetKeyDown (KeyCode.F9)) {
             Load ();
             player.GetComponent<Inventory> ().LoadInventory ();
         }
     }
 
     //NODORSINA KA GameControl IR SINGLETON
     void Awake(){
         if (control == null) {
             DontDestroyOnLoad (gameObject);
             control = this;
         } else if (control != null) {
             Destroy (gameObject);
         }
     }
     //NODORSINA KA GameControl IR SINGLETON
 
 
 
     public void Save(){
         BinaryFormatter bf = new BinaryFormatter ();
         FileStream file = File.Create (Application.persistentDataPath + "/playerInfo.dat");
 
         //SEIT IERAKSTA DATUS KURUS SAGLABAT FAILA!!
         PlayerData data = new PlayerData ();
         data.sceneIndex = sceneIndex;
         data.playerHP = playerHP;
         data.Combatxp = Combatxp;
         data.playerPosX = playerPosX;
         data.playerPosY = playerPosY;
         data.playerPosZ = playerPosZ;
 
         data.currentHealth = currentHealth;
         data.myPosX = myPosX;
         data.myPosY = myPosY;
         data.myPosZ = myPosZ;
 
         //SEIT IERAKSTA DATUS KURUS SAGLABAT FAILA!!
 
         bf.Serialize (file, data);
         file.Close ();
         gameSaved = true;
     }
 
     public void Load(){
         if (File.Exists (Application.persistentDataPath + "/playerInfo.dat")) {
             BinaryFormatter bf = new BinaryFormatter ();
             FileStream file = File.Open (Application.persistentDataPath + "/playerInfo.dat", FileMode.Open);
             PlayerData data = (PlayerData)bf.Deserialize (file);
             file.Close ();
 
             //SEIT IERAKSTA DATUS KURUS IELADET NO FAILA!!
             sceneIndex = data.sceneIndex;
             playerHP = data.playerHP;
             Combatxp = data.Combatxp;
             playerPosX = data.playerPosX;
             playerPosY = data.playerPosY;
             playerPosZ = data.playerPosZ;
 
             currentHealth = data.currentHealth;
             myPosX = data.myPosX;
             myPosY = data.myPosY;
             myPosZ = data.myPosZ;
             //SEIT IERAKSTA DATUS KURUS IELADET NO FAILA!!
 
 
             //SEIT IERAKSTA DATUS KURUS IZSUTIT OBJEKTIEM PEC IELADESANAS NO FAILA!!
             player.GetComponent<PlayerStatsCS>().sceneIndex = sceneIndex;
             player.GetComponent<PlayerStatsCS> ().playerPosX = playerPosX;
             player.GetComponent<PlayerStatsCS> ().playerPosY = playerPosY;
             player.GetComponent<PlayerStatsCS> ().playerPosZ = playerPosZ;
 
             player.GetComponent<PlayerStatsCS> ().playerHP = playerHP;
             player.GetComponent<PlayerStatsCS> ().Combatxp = Combatxp;
 
 
 
             if (enemy != null) {
                 enemy.GetComponent<EnemyScriptCS> ().myPosX = myPosX;
                 enemy.GetComponent<EnemyScriptCS> ().myPosY = myPosY;
                 enemy.GetComponent<EnemyScriptCS> ().myPosZ = myPosZ;
 
                 enemy.GetComponent<EnemyScriptCS> ().currentHealth = currentHealth;
             }
 
             //SEIT IERAKSTA DATUS KURUS IZSUTIT OBJEKTIEM PEC IELADESANAS NO FAILA!!
 
             player.GetComponent<PlayerStatsCS> ().LoadData ();
             enemy.GetComponent<EnemyScriptCS> ().LoadData ();
         }
     }
 
     void OnGUI(){
         if(gameSaved){
             GUI.color = color;
             GUI.Label (new Rect ((Screen.width/2.4f), gameSavedF, 150, 20), "Game saved sucessfully");
             if (gameSavedF >= 130f) {
                 gameSavedF -= 3f;
                 if (gameSavedF < 230 && gameSavedF >= 130) {
                     color.a -= Time.deltaTime;
                 }
             } else { gameSaved = false; gameSavedF = 250; color.a = 1;} 
         }
     }
 }
 
 [Serializable]
 class PlayerData{
 
     //SEIT IERAKSTA MAINIGOS KURU VAJAG SERIALIZET SPELETAJA IERAKSTISANAI!!
     public float playerHP, Combatxp, playerPosX, playerPosY, playerPosZ;
     public int sceneIndex;
     //SEIT IERAKSTA MAINIGOS KURU VAJAG SERIALIZET SPELETAJA IERAKSTISANAI!!
 
     //SEIT IERAKSTA MAINIGOS KURU VAJAG SERIALIZET PRETINIEKA IERAKSTISANAI!!
     public float currentHealth, myPosX, myPosY, myPosZ;
     //SEIT IERAKSTA MAINIGOS KURU VAJAG SERIALIZET PRETINIEKA IERAKSTISANAI!!
 }

The code fufills its function perfectly, but if lets say I saved my game (hp, and coordinates) in Scene A, and went on my way to scene B, if I load the game in Scene B, then it only loads Scene A without actually loading my save game data (hp, and coordinates), but if I press F9 e.g. quickload again it loads fine. Baiscally it works great when loading and saving happens in the same scene. I tried many solutions.

1) tried coroutines to delay scene load 2) tried to load in a loop 2-20 times 3) tried to pass data before scene was loaded, then access load function 4) - || - in a loop

Please help, I am stumped on how to fix this issue.

Thank you a lot in advance!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Get position of ui element on canvas 1 Answer

How to load a script at runtime? 0 Answers

How to judge game Illegal downloaded ? 1 Answer

Is it possible to link to another unity scene from the original scene? 1 Answer

Gameobject goes black when changing material at runtime 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