I am making a simple platformer. I have made the game such that there is a trigger at the bottom of each level tagged levelRestart and whenever player triggers that, i run the levelRestart() function
Now i created a new script called level manager and i added a few functions like takeLife() and giveLife().
using UnityEngine;
public class LivesManagementScript : MonoBehaviour
{
public int lives = 3;
public void takeLife()
{
lives--;
Debug.Log("You have " + lives + " lives left");
}
public void giveLife()
{
lives++;
}
public void giveAllLives()
{
lives = 3;
}
}
I tried referencing these to player script so that i will reduce lives as soon as player collides with level restart layer and will load game over scene if lives == 0
using UnityEngine;
public class BlokScript : MonoBehaviour
{
public SceneManagementScript scene;
public LivesManagementScript lives;
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.tag == "levelRestart")
{
lives.takeLife();
scene.RestartLevel();
if (lives.lives == 0)
{
scene.gameOverScreen();
}
}
else if (collision.gameObject.tag == "loadNextLevel")
{
scene.LoadNextLevel();
}
}
}
But this is not working cause the lives get reduced to 2 but not further from there
Please solve this problem asap
I think the problem might be because it is detecting collisions/triggers only once with each object but no idea on how to fix that