Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
4 captures
13 Jun 22 - 14 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 Kaszanawhot · Apr 13 at 08:55 PM · movementsaveloadpause

Can't move after load saved game

I run the game and save it, then load it and my character cannot move until the pause menu is activated.

loadscript public class Menu1 : MonoBehaviour { public void NewGame() { PlayerPrefs.DeleteKey("p_x"); PlayerPrefs.DeleteKey("p_y"); PlayerPrefs.DeleteKey("TimeToLoad"); PlayerPrefs.DeleteKey("Saved");
SceneManager. LoadScene("GameMain"); }

 public void LoadGame()
 {
     SceneManager.LoadScene("GameMain");
 }

} ============================================= pausescript: { public static bool isGamePaused = false;

 [SerializeField] GameObject PauseMenu;
 
 void Update()
 {
     if (Input.GetKeyDown(KeyCode.Escape))
     {
         if (isGamePaused)
         {
             ResumeGame();
         }
         else
         {
             PauseGame();
         }
     }
 }



 public void ResumeGame()
 {
     PauseMenu.SetActive(false);
     Time.timeScale = 1f;
     isGamePaused = false;
 }

 void PauseGame()
 {
     PauseMenu.SetActive(true);
     Time.timeScale = 0f;
     isGamePaused = true;
 }

} =================================================

savingscript: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement;

public class Quit : MonoBehaviour {

 SavePlayerPos playerPosData;

 void Start()
 {
     playerPosData = FindObjectOfType<SavePlayerPos>();
 }

 public void Menu()
 {
     playerPosData.PlayerPosSave();
     SceneManager.LoadScene("Menu");
 }

} ============================================================= savingposplaer:

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

public class SavePlayerPos : MonoBehaviour { public GameObject player;

 private void Start()
 {
     if (PlayerPrefs.GetInt ("Saved") == 1 && PlayerPrefs.GetInt("TimeToLoad") == 1)
     {
         float pX = player.transform.position.x;
         float pY = player.transform.position.y;

         pX = PlayerPrefs.GetFloat("p_x");
         pY = PlayerPrefs.GetFloat("p_y");
         player.transform.position = new Vector2(pX, pY);
         PlayerPrefs.SetInt("TimeToLoad", 0);
         PlayerPrefs.Save();
     }
 }

 public void PlayerPosSave ()
 {
     PlayerPrefs.SetFloat ("p_x", player.transform.position.x);
     PlayerPrefs.SetFloat("p_y", player.transform.position.y);
     PlayerPrefs.SetInt("Saved", 1);
     PlayerPrefs.Save();
 }

 public void PlayerPosLoad()
 {
     PlayerPrefs.SetInt("TimeToLoad", 1);
     PlayerPrefs.Save();
 }

}

Comment
Add comment · Show 1
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 sandbaydev · Apr 14 at 03:48 AM 0
Share

Not sure but PlayerPrefs.GetInt ("Saved") == 1 can easily fail since you do not necessarily have such information stored. It's a good idea to check PlayerPrefs.HasInt(keyname_here) before doing that.

It might be just me, but I would not use "PlayerPrefs.Get/Set" functions for "TimeToLoad". I would add SaveManager class or such, and would put all those things as variables (int timeToLoad = 0 etc) and then check against those.

Now, to solve your problem: make sure you set "isGamePaused" to true or false in the Start() or somewhere.

PlayerPrefs saving is not necessarily "instant". I am not 100% sure, but it might be that be causing issues when you think you have variable information, but in reality it might arrive frame later. So, make sure you:

  • use variables like "int timeToLoad"

  • use GetInt and SetInt only when loading/saving data, not within Update/Start

  • Make sure you have game state (paused or whatever) stored in a variable, and set that variable in Start

Does this help anything?

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

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

how do you save and make a pause menu? 3 Answers

[Solved] PlayerPrefs Vector 3 Defaulting to 0,0,0 1 Answer

What is best way to save several arrays into a file? 0 Answers

Problem with saving and loading game timer 0 Answers

Get a NaN error when unpausing a game 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