Jump to content
  • Advertisement
Sign in to follow this  

Save and load scene in unity

This topic is 719 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i writing my code for the save game progress, i able to save my player position and player score and health. I want my save game load from the main menu. Anyone can help me? i got 4 scene which is 1 main menu and 3 level.



using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class SaveandLoadScript : MonoBehaviour {
    public Transform player;
    public Transform enemy;
    public int score = 0;
    public int[] SceneID =new int[]{'0','1','2','3'};
    public void save()
        PlayerPrefs.SetFloat("PlayerPosX", player.position.x);
        PlayerPrefs.SetFloat("PlayerPosY", player.position.y);
        PlayerPrefs.SetFloat("PlayerPosZ", player.position.z);
        PlayerPrefs.SetFloat("EnemyPosX", enemy.position.x);
        PlayerPrefs.SetFloat("EnemyPosY", enemy.position.y);
        PlayerPrefs.SetFloat("EnemyPosZ", enemy.position.z);
        PlayerPrefs.SetInt("SceneID", SceneManager.GetActiveScene().buildIndex);
    public void Load()
        PlayerPrefs.GetInt("Score", 0);
        PlayerPrefs.GetInt("SceneID", SceneManager.GetActiveScene().buildIndex);
        player.position = new Vector3(PlayerPrefs.GetFloat("PlayerPosX"), PlayerPrefs.GetFloat("PlayerPosY"), PlayerPrefs.GetFloat("PlayerPosZ"));
        enemy.position = new Vector3(PlayerPrefs.GetFloat("PlayerPosX"), PlayerPrefs.GetFloat("PlayerPosY"), PlayerPrefs.GetFloat("PlayerPosZ"));
Edited by pch93

Share this post

Link to post
Share on other sites

i added this into my save function, but the problem is i cannot load my game in the main menu...and another problem is with this code i no able to save many enemies. it just save one enemy, can you help me with that?

Share this post

Link to post
Share on other sites

For Finding multiple enemies, I hope you tagged them as "Enemy", or something similar to that, with that in mind:

(Oh, according to Unity you need to write an abstraction Layer to PlayerPrefs to save multiple so think about doing it as a peasant)

  1. foreach(GameObject ObjectToSave in GameObject.FindGameObjectsWithTag("Enemy"))
  2. {
  3. StreamWriter sw = new StreamWriter(@"SaveFile.s");
  4. sw.WriteLine(ObjectToSave.Name + " - " + ObjectToSave.transform.Location.X + " - " + ObjectToSave.transform.Location.Y +
  5. " - " + ObjectToSave.transform.Location.Z + " - " + ObjectToSave.transform.Quaternion.eulerAngles);
  6. }
  7. sw.Close();

Add StreamReader to read the save file from main menu, lets say Line one to point the scene and set bool Load for instance for scene Main Controllers cript to know to read the rest of the file, then just do the same thing you did for saving only in reverse to return everything the way it was in the scene.


Or run it like XML file that is a lot more elegant.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!