Hey everyone noob question but so am i! Anyways i am trying to make a game and trying to find my own way around. so i got to a point i implement lives to my character and in my gamemanager i want to say when lives <= 0 game over and play the players death animation. I ised to have the death animation in the collider but now since i want to have lives i dont know how to make it work.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class GameManager : MonoBehaviour
{
public static GameManager instance;
public GameObject PlayerDeath;
public GameObject livesHolder;
public GameObject gameOverPanel;
public Text scoreText;
int score = 0;
int lives = 5;
// Start is called before the first frame update
private void Start()
{
gameOverPanel.SetActive(false);
}
private void Awake()
{
if (instance == null)
{
instance = this;
}
}
// Update is called once per frame
void Update()
{
}
public void GameOver()
{
ObstacleSpawner.instance.gameOver = true;
StopScrolling();
gameOverPanel.SetActive(true);
}
void StopScrolling()
{
TextureScroll[] scrollingObjects = FindObjectsOfType<TextureScroll>();
foreach (TextureScroll t in scrollingObjects)
{
t.scroll = false;
}
}
public void Restart()
{
SceneManager.LoadScene("Game");
}
public void Menu()
{
SceneManager.LoadScene("Menu");
}
public void IncrementScore()
{
score ++;
if(scoreText == null) Debug.LogError("scoreText not provided, click this message", gameObject);
else scoreText.text = score.ToString("0");
}
public void DecreaseLife()
{
if (lives > 0)
{
lives--;
livesHolder.transform.GetChild(lives).gameObject.SetActive(false);
}
else if (lives <= 0)
{
//PlayerController Death = PlayerDeath.GetComponent<Death>();
PlayerController.GetComponent.Death();
PlayerController.Death();
//GameManager.instance.GameOver();
//GameOver();
}
}
}
this was my gamemanager and here is my playercontroller
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
Rigidbody2D rb;
public Animator anim;
bool grounded;
public bool gameOver = false;
[SerializeField]
float jumpForce;
// Start is called before the first frame update
private void Awake()
{
rb = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0) && !gameOver)
if (grounded)
{
{
Jump();
}
}
}
void Jump()
{
grounded = false;
rb.velocity = Vector2.up * jumpForce;
anim.SetTrigger("Jump");
GameManager.instance.IncrementScore();
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "Ground")
{
grounded = true;
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
if(collision.gameObject.tag == "Obstacle")
{
Destroy(collision.gameObject);
GameManager.instance.DecreaseLife();
}
}
public void Death()
{
anim.Play("PlayerDie");
GameManager.instance.GameOver();
gameOver = true;
}
}