Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Resolved How to save current scene play is in with playerprefs?

Discussion in 'Scripting' started by nckazzi, Jun 10, 2020.

  1. nckazzi

    nckazzi

    Joined:
    Jun 10, 2020
    Posts:
    4
    Is it possible to load a scene with playerprefs with a save and load button? I just need it to load in the scene that the player was last in once they press the save button and load the scene with the load button. Is that possible or would I have to use something else?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,079
  3. nckazzi

    nckazzi

    Joined:
    Jun 10, 2020
    Posts:
    4
    I don't think thats it, what I'm trying to say is when I click the save button it saves just the scene I am in and when I click the load button it loads that scene.
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,933
    Like, saves the current state of the scene and the state of all the objects inside it? There's nothing that will do that automatically for you. You need to build your own save/load system.
     
  5. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    If you're just trying to save which scene the player is in, and not the entire state of the world, then you can save the scene name or number and use that.
     
    nckazzi and PraetorBlue like this.
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,079
    Yeah, like Praetor said above, that's a thing you make, but luckily there's like fifty billion (at least) Youtube tutorials on doing it. Work through one or two and get a feel for what's involved.
     
  7. nckazzi

    nckazzi

    Joined:
    Jun 10, 2020
    Posts:
    4
    I was looking for what antistone is talking about not what you guys but I think I found a way to do it so thanks for the help.
     
  8. nckazzi

    nckazzi

    Joined:
    Jun 10, 2020
    Posts:
    4
    I found out how to do it this code did the trick thanks to Antistone thx bro!!!

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class save : MonoBehaviour
    7. {
    8.  
    9.     public void SaveGame()
    10.     {
    11.         PlayerPrefs.SetInt("Level", SceneManager.GetActiveScene().buildIndex);
    12.         PlayerPrefs.Save();
    13.         print("Game saved!");
    14.     }
    15.  
    16.     public void LoadGame()
    17.     {
    18.         SceneManager.LoadScene(PlayerPrefs.GetInt("Level"));
    19.         print("Game loaded!");
    20.     }
    21. }
     
  9. shansajusaho

    shansajusaho

    Joined:
    Sep 1, 2020
    Posts:
    13
    This is awesome. Thank you @nckazzi