Two game development questions
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);
}
}
Answer by tutaBugarin · Oct 07, 2020 at 08:29 PM
@burchland2 It has already been discussed here: https://gamedev.stackexchange.com/questions/110958/what-is-the-proper-way-to-handle-data-between-scenes#:~:text=Unity%20has%20a%20built%20in,types%20string%20%2C%20int%20and%20float%20 (it works for both 2d and 3d). Hope that helps! If that doesn't work or you don't understand something, feel free to ask!
Your answer
Follow this Question
Related Questions
Can not access GetComponentInParent ( ). usegravity 1 Answer
Error CS1519 1 Answer
Animation won't stop (Solved) 2 Answers