Load game save ?

I have two scenes ((Menu & game level))
Menu have a button to load “game save” from another script (script use in the game level scene)
Menu script :

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class MainMenu : MonoBehaviour {
  
        public save&load _p;

        public Canvas Canvmenu;
        public Button Last_Save;

        void Start()

        {
            Canvmenu = Canvmenu.GetComponent<Canvas>();
            Last_Save = GetComponent<Button>();
        }

        void Update()
        {

        }

        public void Last_sav()
        {
        _p.loadposition();
        }
    }

this is the save & load file, here i used a simple code, without use “Serializable” or “System.IO”:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class save&load : MonoBehaviour {
 
    public void saveposition()
    {
        PlayerPrefs.SetFloat("posX", transform.position.x);
        PlayerPrefs.SetFloat("posY", transform.position.y);
        PlayerPrefs.SetFloat("posZ", transform.position.z);
       
        PlayerPrefs.SetFloat("rotX", transform.eulerAngles.x);
        PlayerPrefs.SetFloat("rotY", transform.eulerAngles.y);
        PlayerPrefs.SetFloat("rotZ", transform.eulerAngles.z);
        Debug.Log("save");
    }

    public void loadposition()
    {
        float x = PlayerPrefs.GetFloat("posX");
        float y = PlayerPrefs.GetFloat("posY");
        float z = PlayerPrefs.GetFloat("posZ");

        float rx = PlayerPrefs.GetFloat("rotX");
        float ry = PlayerPrefs.GetFloat("roty");
        float rz = PlayerPrefs.GetFloat("rotz");

        transform.position = new Vector3(x, y, z);
        transform.rotation = Quaternion.Euler(rx, ry, rz);
        Debug.Log("load");

    }

    public void deleteall()
    {
        PlayerPrefs.DeleteAll();
    }
}

So why i can’t load the game level scene when i press the button ?
any advice ?

The first thing that comes to mind is that the Start method in your MainMenu class is unnecessary, if you’re assigning the Canvmenu and Last_Save as public variables. Those lines will override whatever you’ve assigned to those variables.

More importantly, I suspect that the Last_Save Button doesn’t actually have an action assigned to it. Check it in the Inspector; if it doesn’t, then add an OnClick method and see if it works.

Philip’s probably right. But it’d be helpful if you told us more of your observations. For example, you were smart enough to put a Debug.Log into your save/load methods. That’s always a great first step; you should be asking yourself, is this method not working, or is it not being called at all? The Debug.Log will tell you that (and you should have told us).

Also, I’m a little shocked that “save&load” is a valid C# name. Even if it is, please don’t do it… & is a C# operator, and using it in an identifier or class name is certainly going to confuse human readers, if not the compiler.

1 Like

Thank you for your respond.

Good point. I will do what you said.

No. I did it. All my buttons ((New Game, Option, Exit)) run well except Last Save.
I need to write a code telling the player in which (Chapter or Level) Scene.
This code run well if i put it in the in the first game level. No problem at all. but there is no line telling me or game engine what (chapter or Level) scene of the game is this ?!!

Example:
You make a game have 3 scenes (Main Menu, First Level, Boss Level). you fight the boss and die. Now you return to Main Menu and press Last-Save button. can you tell me what level this code will load ? this code can not tell you last level you played, only tell you the position of the player.

you right. D.log is really helpful.

I change the name to make it short when post it here :smile:
Sorry.

Well you need to save that information in the playerprefs, too. If you’re using the (now deprecated, but as far as I can tell, not completely replaced) Application methods to manage your levels, then use Application.loadedLevel to get the current level number and store it in the prefs. You can do this in any Start method in your game levels. Then when you want to restore, just pull that number out of the prefs, and pass it to Application.LoadLevel.

This is my first time do this. do you have any example or tutorial ?

No, but you’re already using SetFloat and GetFloat… this is the same thing, but with SetInt and GetInt.

What i understand, I’m going to use numbers to refer to the scenes, right ?
if (mylevel = 1)
{
application.loadlevel(“level_1”);
}

LoadLevel can take a level number or a level name. You’re going to use the number.

Application.LoadLevel(PlayerPrefs.GetInt("last_level_num"));

Look. after updating my engine loadlevel changed to scenemanger.
I wrote a code by using String to define a level name. it work and give me the name of scene. but i can’t save or load !!
Here is my code:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class saveload : MonoBehaviour {
 
    public CountEat _ec;
    public string mylevel;

    void Start()
    {
        mylevel = SceneManager.GetActiveScene().name;
    }

    public void saveposition()
    {
        SceneManager.GetActiveScene(PlayerPrefs.SetString("now_level",mylevel)); /// error CS1501: No overload for method `GetActiveScene' takes `1' arguments
        Debug.Log("save");
    }

    public void loadposition()
    {
        string level = SceneManager.LoadScene(PlayerPrefs.GetString("now_level")); /// error CS0029: Cannot implicitly convert type `void' to `string'
        Debug.Log("load");
    }

@Quast , have you gone through any C# programming lessons or tutorials?

I’m thinking you haven’t, and you’re just trying to figure it out as you go along, but it’s going to be a really long slog, because you’re missing very basic stuff like whether a function returns a value, and how to correctly use that return value for something else.

I’ve heard the online exercises at learncs.org are pretty good. I’ve also heard good things about Udemy courses, like this one.

I would say, start with those, and then come back to this. Look at what those methods you’re using get and return, and you’ll see how to chain them together properly.

1 Like