Hi, guys.
I need some help. How can I properly:
- let a player lose a life after falling into water, store the data, and let the player try again with fewer lives left
- and collect coins, earn points, and save the data between stages?
Here is the relevant part of the code.
private void OnTriggerEnter2D(Collider2D other)
{
lives = 9;
lifeboard.text = "Lives: " + lives;
if (other.gameObject.tag == "coin")
{
myAudioSource.PlayOneShot(cent);
Destroy(other.gameObject);
score += 10;
scoreBoard.text = "Score: " + score;
if (score >= 100)
{
lives = lives + 1;
lifeboard.text = "Lives: " + lives;
}
DontDestroyOnLoad(this.gameObject);
}
if (other.gameObject.tag == "goal")
{
myAudioSource.PlayOneShot(net);
Time.timeScale = 0;
results.text = "Mission Completed." + Environment.NewLine + "Press Space to continue.";
if (Input.GetButtonDown("Jump"))
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
DontDestroyOnLoad(this.gameObject);
}
if (other.gameObject.tag == "water")
{
myAudioSource.PlayOneShot(splash);
Time.timeScale = 0;
results.text = "Mission Failed." + Environment.NewLine + "Press Esc to restart.";
lives = lives - 1;
lifeboard.text = "Lives: " + lives;
if (Input.GetButtonDown("Cancel"))
{
Scene thisScene = SceneManager.GetActiveScene();
SceneManager.LoadScene(thisScene.name);
}
DontDestroyOnLoad(this.gameObject);
}
}