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 Aleandrus · Mar 14, 2014 at 03:20 PM · javascriptscenesaveload

How to load saved data from another scene?

I'm making a third person game of space exploration.

Actualy, I made an InGameMenu that let the user save and load his game and this it's working corretly, using the following Javascript:

 function Update ()
 {
     if(Input.GetKeyDown(KeyCode.Escape))
     {
         openMenu = true;
     }
     PlayerX =(PlayerPosition.transform.position.x);
     PlayerY =(PlayerPosition.transform.position.y);
     PlayerZ =(PlayerPosition.transform.position.z);
 }
 
 function OnGUI ()
 {
     if(openMenu == true)
     {
         if (GUI.Button(Rect(Screen.width/2-50,180,boxX,boxY), "Salva")) //Save game
         {
             Debug.Log("Salva");
             saveAttributes();
             saveText.enabled = true;
 
         }
         if (GUI.Button(Rect(Screen.width/2-50,260,boxX,boxY), "Carica")) //Load Game
         {
             Debug.Log("Carica");
             loadstuff();
         }
         if (GUI.Button(Rect(Screen.width/2-50,340,boxX,boxY), "Torna al menu"))
         Application.LoadLevel("mainMenu"); //Return to MainMenu
         
         if (GUI.Button(Rect(Screen.width/2-50,420,boxX,boxY), "Chiudi")) //Close InGameMenu
         openMenu = false;
     }
 }
 
 // a function created to save a game
 function saveAttributes()
 {
     if(saveOn == true)
         PlayerPrefs.SetInt("SCurPe", shipStats.curPe);  //Experience Points
         PlayerPrefs.SetInt("SCurPc", shipStats.curPc);  //Life
         PlayerPrefs.SetInt("SCurPs", shipStats.curPs);  //Shield
         PlayerPrefs.SetInt("SLevel", shipStats.level);  //Level of the player
         PlayerPrefs.SetFloat("PlayerX",PlayerPosition.transform.position.x); //position on X
         PlayerPrefs.SetFloat("PlayerY",PlayerPosition.transform.position.y); //position on Y
         PlayerPrefs.SetFloat("PlayerZ",PlayerPosition.transform.position.z); //position on Z
         Debug.Log("Salvato");
 }
 
 // a function created to load a game
 function loadstuff()
 {
     if(loadOn == true)
         Debug.Log("loaded");
         shipStats.curPe = PlayerPrefs.GetInt("SCurPe");
         shipStats.curPc = PlayerPrefs.GetInt("SCurPc");
         shipStats.curPs = PlayerPrefs.GetInt("SCurPs");
         shipStats.level = PlayerPrefs.GetInt("SLevel");
         PlayerPosition.transform.position.x = (PlayerPrefs.GetFloat("PlayerX"));
         PlayerPosition.transform.position.y = (PlayerPrefs.GetFloat("PlayerY"));
         PlayerPosition.transform.position.z = (PlayerPrefs.GetFloat("PlayerZ"));
         Debug.Log("Caricato");
 }


But I've a problem when I try to load that saved data from another scene, in particular case, from my MainMenu:

 #pragma strict
 
 var boxX : float = 100;
 var boxY : float = 50;
 var saveOn : boolean = false;
 var loadOn : boolean = false;
 
 private var PlayerX : float;
 private var PlayerY : float;
 private var PlayerZ : float;
 var PlayerPosition : Transform;
 
 function Update ()
 {
     PlayerX =(PlayerPosition.transform.position.x);
     PlayerY =(PlayerPosition.transform.position.y);
     PlayerZ =(PlayerPosition.transform.position.z);
 }
 
 function OnGUI ()
 {
     {
         if (GUI.Button(Rect(Screen.width/2-100,180,boxX,boxY), "Nuova Partita")) //New Game
         {
             Application.LoadLevel("Universe");
         }
         if (GUI.Button(Rect(Screen.width/2-100,260,boxX,boxY), "Carica Partita")) //Load Game
         {
             loadstuff();
         }
         if (GUI.Button(Rect(Screen.width/2-100,340,boxX,boxY), "Chiudi")) //Close the game
         {
             Application.Quit();
         }
     }
 }

 //Function to load the game
 function loadstuff()
 {
     if(loadOn == true)
         Debug.Log("loaded");
         shipStats.curPe = PlayerPrefs.GetInt("SCurPe");
         shipStats.curPc = PlayerPrefs.GetInt("SCurPc");
         shipStats.curPs = PlayerPrefs.GetInt("SCurPs");
         shipStats.level = PlayerPrefs.GetInt("SLevel");
         PlayerPosition.transform.position.x = (PlayerPrefs.GetFloat("PlayerX"));
         PlayerPosition.transform.position.y = (PlayerPrefs.GetFloat("PlayerY"));
         PlayerPosition.transform.position.z = (PlayerPrefs.GetFloat("PlayerZ"));
         Debug.Log("Caricato");
 }

When I press "Nuova partita", it'll start correctly a new game; if I press "Chiudi", it'll close the game, but, when I press "Carica", nothing happen. If I try to start a game and then use the InGameMenu to load the saved game, it work perfectly, so it's just a problem of the MainMenu.

Any idea on how to correct the script to make it possibile to load the saved game, from the MainMenu?

Comment
Add comment · Show 3
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 Ardito · Apr 26, 2014 at 02:12 PM 0
Share

You have solved it? I have the same problem.

avatar image poncho · Apr 26, 2014 at 02:19 PM 0
Share

you could A, write a file with that info(serialization the best option), B, have a persisting game object (DontDestroyOnLoad(mygameobject);), C,update to a web database and fetch it back on the next scene, etc..

avatar image Aleandrus · Apr 27, 2014 at 05:54 AM 0
Share

In the end I used the Unity Serializer 2.0(http://whydoidoit.com/unityserializer/) to solve my problem; I had to work a little on it to satisfy my needs, but it's quite versatile and easy to use.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Sisso · Apr 26, 2014 at 02:27 PM

With a fast look into the code appear to be "ok", but there is a ton of flaws that could failed. Add more logs in your code, check your console for erros, and debug step by step your code.

Line 41, loadOn could be false and Debug.Log("loaded") never called?

Line 15, you updated PlayerX, but how read it?

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
avatar image
0

Answer by TheLolzguy · Apr 27, 2014 at 07:43 AM

Save the level as a string and then call Application.Load(string of the saved level) in the loadstuff function in the the main menu scene.

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

24 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

Related Questions

Saving and Loading an Object and it's Children in Javascript 2 Answers

Save & Load Game question 3 Answers

Remembering the state of a scene 1 Answer

checkpoint level please 2 Answers

How can i save/load a changing variable in a scene file?(EX:highscore/name) 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