How to save player's lives using playerprefs while I load the same scene?

I made a 2D game in unity.

And I set a trap when the player has a collision on it, it will dead and can’t move.

Meanwhile,I load the same scene to make my character reborn and reset the objects.

I set the key “Tab” to use SceneManager.loadscene.

But I don’t know where to set the text about number of lives.

I use PlayerPrefs to set and get int,but it seems that the number will be reseted when the scene reload.

When I start the game and make the player die,while I press Tab,it becomes 2 but turn back to 3 in a second.

Is it because the scene reload?

Script 1:

public class MainMenu : MonoBehaviour
{
public int currentlive;

    public void NewGame()
    {
    PlayerPrefs.SetInt("currentLives", currentlive);
    }
}

Script 2:

void Start()
{
    life = PlayerPrefs.GetInt("currentLives");
}
void OnCollisionEnter2D(Collision2D collision)
{
    if (collision.gameObject.tag=="Trap")
        {
            object1.SetActive(true); //Show Trap

            run = false;
            jump = false;
            dead = true;  //animations

            lifecounter--;  //lives decrease
            PlayerPrefs.SetInt("currentLives", lifecounter);
        }
}

Script 3:

void Update();
{
    if (dead == true)  //start the dead animation
        {
            player.GetComponent<Player>().enabled = false;  //To make the player can't move
            if (Input.GetKeyDown(KeyCode.Tab))
            {
                EditorSceneManager.LoadScene(EditorSceneManager.GetActiveScene().name); //reload scene
                Life.text = "X " + PlayerPrefs.GetInt("currentlife"); //show the lives on the game
            }
        }
}

I understand what you are trying to do, but you can do it in more efficient ways. There is one way that comes to mind


Create a game object that has a game manager script on it. It’ll be used to save player lives and such. Create an Awake method with the line of code

DontDestroyOnLoad(this.GameObject);

This is used to keep the game object from being deleted between scenes. So any variables saved won’t be restarted. You then can destroy the game object when going to the menu using Destroy(game manager gameobject) inserting what ever you need to destroy it.
The last part you will need to know is to prevent the gameobject from being set a 2nd time. Best option is to find the gameobject using GameObject.Find(gameobject name) and checking to see if one exists, if it doesn’t, then spawn it in.


Setting the text to show lives can be easy too. If you are looking for a fast an easy way, just create a Text variable to put the UI gameobject in, then in the update method, put the following

livesTxt.text = "Lives: " + lives;

Or something similar to that. Best option is to put it in the gamemanager script too so you don’t have to call it

If you still don’t understand, I can create an example script for you to show you how to get the code to work

But why playerprefs? Just use a static variable.