Hi, my game doesnt have main menu, just simply levels, i want create save system so when players opens the game i want to continue from last session scene(level) . here is my game code
public class PlatformBuild : MonoBehaviour
{
void Awake()
{
LoadLevel();
}
public void LoadLevel()
{
float spawnPosZ = -42.86f;
for (int i = 0; i < PlatformsSize; i++)
{
GameObject platform = Instantiate(RandomPlatform, transform);
platform.transform.localPosition = new Vector3(0, 0, spawnPosZ);
platform.transform.localEulerAngles = new Vector3(0, 0, UnityEngine.Random.Range(0, 360));
spawnedPlatforms.Add(platform);
}
GameObject finalPlatformInstance = Instantiate(finalPlatform, transform);
finalPlatformInstance.transform.localPosition = new Vector3(0, 0, spawnPosZ);
}
}
this is my gameManger script, here in this script i tried to save scene and load but it didnt work.
here i used playeprefs, i know it is not recommended to save scene but i dont know how save in binary files.
public class GameManager : MonoBehaviour
{
private void Start()
{
LoadGame();
}
public void SaveScene()
{
PlayerPrefs.SetInt("Level", SceneManager.GetActiveScene().buildIndex);
PlayerPrefs.Save();
}
public void LoadGame()
{
sceneToContinue = PlayerPrefs.GetInt("Level");
if (sceneToContinue != 0)
{
SceneManager.LoadScene(sceneToContinue);
}
else
return;
}
public void NextLevel()
{
LevelCompletedMenu.SetActive(false); StartCoroutine(LoadFade(SceneManager.GetActiveScene().buildIndex + 1));
}
public void RestartLevel()
{
score = 0;
StartCoroutine(LoadReStart());
GameOver.SetActive(false);
StartCoroutine(LoadFade(SceneManager.GetActiveScene().buildIndex));
}
}