I’m trying to get my number of lives to update properly. Here is my code:
using UnityEngine.SceneManagement;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DetectCollision:MonoBehaviour
{
int score,nbLives,nbCoinsCollectedPerLevel;
// Start is called before the first frame update
void Start()
{
nbCoinsCollectedPerLevel = 0;
}
// Update is called once per frame
void Update()
{
}
void OnCollisionEnter2D(Collision2D coll)
{
string tagName=coll.collider.gameObject.tag;
if(tagName=="pick_me")
{
Destroy(coll.collider.gameObject);
//score++;
score = PlayerPrefs.GetInt("score");
score++;
PlayerPrefs.SetInt("score",score);
nbCoinsCollectedPerLevel++;
if(SceneManager.GetActiveScene().name=="level1"&&nbCoinsCollectedPerLevel>=5)
{
SceneManager.LoadScene("level2");
}
print("score" + score);
}
if (tagName == "avoid_me" || tagName == "reStarter")
{
Destroy(coll.collider.gameObject);
nbLives = PlayerPrefs.GetInt("nbLives");
nbLives--;
PlayerPrefs.SetInt("nbLives", nbLives);
if (nbLives >= 0)
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
else SceneManager.LoadScene("lose");
print ("lives" + nbLives);
}
if(tagName=="endOfLevelTwo")
{
SceneManager.LoadScene("win");
}
}
}
And here is the code that loads the lives in the splash screen scene:
using UnityEngine;
using System.Collections;
public class initGame : MonoBehaviour {
// Use this for initialization
void Start ()
{
PlayerPrefs.SetInt ("score", 0);
PlayerPrefs.SetInt ("nbLives", 3);
}
// Update is called once per frame
void Update () {
}
}
What is wrong with my code?
Please help.
Thanks.
nstruth