- Home /
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();
}
}
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?
Your answer
Follow this Question
Related Questions
how do you save and make a pause menu? 3 Answers
How can I save and read data on Android? 1 Answer
Save and Load plus a few other stuff 3 Answers
save and load renderer data on a unit composed prefab 0 Answers
Save / Load high score online 2 Answers