Melin Ven Melin Ven - 3 months ago 26
C# Question

Save hearts with PlayerPrefs

I am building a 2d asteroid shooter game in unity c#. What I need is saving the hearts of the player for the next level. So I just made the score save what I need now is heart saving. The score is saved for the next level, so it works like this, in first level you have for example at the end 3000 points and you go to the nesxt level, then it will start form 3000, but if you click on restart or if oyu quit you start from 0 and fro first level. That's how the score works the same needs to be with the hearts. So if you finish first level with 1 heart then you need to go to 2 with 1 heart. if you finish 2 with 3 hearts tn 3 with 3 hearts will be started , if ou click restart or if you quit then it will start form default. The hearts are images UI, I was thinking to do like the scor with player prefs but I am not sure how.

if (col.gameObject.tag == "AsteroidBig") {
if (heart.enabled == true && heart1.enabled == true && heart2.enabled == true) {
heart.enabled = false;
Instantiate (explosion, col.transform.position, col.transform.rotation);
Vector3 pos = new Vector3 (0f, 0f, 0f);
Instantiate (player, pos, Quaternion.identity);
Destroy (col.gameObject);
Destroy (this.gameObject);
Instantiate (medast[Random.Range(0,medast.Length)],col.transform.position,col.transform.rotation);
Instantiate (medast[Random.Range(0,medast.Length)],col.transform.position,col.transform.rotation);
} else if (heart1.enabled == true && heart2.enabled == true) {
heart1.enabled = false;
Instantiate (explosion, col.transform.position, col.transform.rotation);
Vector3 pos = new Vector3 (0f, 0f, 0f);
Instantiate (player, pos, Quaternion.identity);
Destroy (col.gameObject);
Destroy (this.gameObject);
Instantiate (medast[Random.Range(0,medast.Length)],col.transform.position,col.transform.rotation);
Instantiate (medast[Random.Range(0,medast.Length)],col.transform.position,col.transform.rotation);
} else if (heart2.enabled == true) {
heart2.enabled = false;
Instantiate (explosion, col.transform.position, col.transform.rotation);
Vector3 pos = new Vector3 (0f, 0f, 0f);
Instantiate (player, pos, Quaternion.identity);
Destroy (col.gameObject);
Destroy (this.gameObject);
Instantiate (medast[Random.Range(0,medast.Length)],col.transform.position,col.transform.rotation);
Instantiate (medast[Random.Range(0,medast.Length)],col.transform.position,col.transform.rotation);
} else if (heart.enabled == false && heart1.enabled == false && heart2.enabled == false) {
Instantiate (explosion, col.transform.position, col.transform.rotation);
Destroy (col.gameObject);
Destroy (this.gameObject);
Instantiate (medast[Random.Range(0,medast.Length)],col.transform.position,col.transform.rotation);
Instantiate (medast[Random.Range(0,medast.Length)],col.transform.position,col.transform.rotation);
GameOverPanel.SetActive (true);
}
}


An if statement with hearts looks like that, it is a bit messed up, but it is like this. If you touch the asteroid it checks if 3 hearts are on, one of them is seting to false, then if you have 2 hearts on, then another one is going to false, and so on, till you have no hearts and you loose. That is th logic of the collision. Please help to save this hearts, ask any question becaue I know it is messed up.

Answer

You have two options here, depending on what's easier for you

1) Create a static class Static classes aren't reset when you load a new level, so you can store any information about your player there

public class GameData
{
    public static int NumberOfHearts = 3;
}

In your code you would then update NumberOfHearts to reflect how many hearts you have when you get touched by an asteroid. For example,

GameData.NumberOfHearts--; //We just got touched by an asteroid

2) Scene Manager (doesn't apply if you're using Application.LoadLevel())

You could create a GameObject for your player at the root of the scene.
Then when it's time to load your level, you would call SceneManager.MoveGameObjectToScene(playerObject, "sceneName")

See here for more information about MoveGameObjectToScene.

In this case, the object would be carried over with any scripts and subobjects, so you would already have all the hearts in the next level.