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 /
This question was closed Jan 02, 2017 at 08:27 PM by Asellio for the following reason:

wasnt anwsered

avatar image
0
Question by Asellio · Dec 28, 2016 at 08:41 AM · savesavingsaveload

Can only Save in on scene

I am only able to save in one scene and when i try to save in any other scene it either doesnt work or teleports me off the map. public class GameControl : MonoBehaviour {

     public static GameControl control;
     public Transform player;
     
     private float x;
     private float y;
     private PlayerStats thePS;
     private PlayerHealthManager pHM;
     private string newUserName;
 
     void Awake () {
         if(control == null)
         {
             DontDestroyOnLoad(gameObject);
             control = this;
         }else if(control != this)
         {
             Destroy(gameObject);
         }
     }
     
     private void Start()
     {
         player = GameObject.FindGameObjectWithTag("Player").transform;
         thePS = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerStats>();
         pHM = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerHealthManager>();
     }
     
     public void Save()
     {
         PlayerPrefs.SetInt("Level", SceneManager.GetActiveScene().buildIndex);
         PlayerPrefs.SetFloat("PlayerX", player.transform.position.x);
         PlayerPrefs.SetFloat("PlayerY", player.transform.position.y);
         Debug.Log("GameSaved");
         print(player.transform.position.x);
         print(player.transform.position.y);
         if (!Directory.Exists("Saves"))
             Directory.CreateDirectory("Saves");
 
         BinaryFormatter bf = new BinaryFormatter();
         FileStream file = File.Create(Application.persistentDataPath + "/playerInfo.dat");
 
         PlayerData data = new PlayerData();
         data.currentHealth = pHM.playerCurrentHealth;
         data.maxHealth = pHM.playerMaxHealth;
         data.level = thePS.currentLevel;
 
         bf.Serialize(file, data);
         file.Close();
     }
     public void Load()
     {
         SceneManager.LoadScene(PlayerPrefs.GetInt("Level"));
         player.transform.position = new Vector2(PlayerPrefs.GetFloat("PlayerX"), PlayerPrefs.GetFloat("PlayerY"));
 
         print(player.transform.position.x);
         print(player.transform.position.y);
         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);
 
             pHM.playerCurrentHealth = data.currentHealth;
             pHM.playerMaxHealth = data.maxHealth;
             thePS.currentLevel = data.level;
             
 
 
             file.Close();
         }
     }
 }
 
 [Serializable]
 class PlayerData
 {
     public int currentHealth;
     public int maxHealth;
     public int level;
 }
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

1 Reply

  • Sort: 
avatar image
0

Answer by Socapex · Dec 28, 2016 at 06:46 PM

Are the other scenes turned on in the build settings? You can try saving the scene name instead, and use the SceneManager.LoadScene overload that takes a string parameter.

Comment
Add comment · Show 2 · 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 Asellio · Dec 28, 2016 at 07:01 PM 0
Share

ya the other scenes are turned on.

avatar image Asellio · Dec 28, 2016 at 08:59 PM 0
Share

also how would i save the scene to then load it would it be Scene$$anonymous$$anager.GetActiveScene?

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Problem with saving and loading game timer 0 Answers

How do I make a basic easy Save and load function with JavaScript 1 Answer

Saving state across the scene シーンをまたぐ状態の保存 0 Answers

,Block Levels and save progress 1 Answer

How do I quickly save a large number of variables? 5 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