what is wrong with this???? game is not restarting when hp is zero
using UnityEngine;
public class PlayerCollision : MonoBehaviour { public GameManager gameManager; public float health = 100;
void Start ()
{
}
void OnCollisionEnter (UnityEngine. Collision collisionInfo)
{
if (collisionInfo.gameObject.tag == "Obstacle")
{
float health = 100;
Debug.Log ("-100");
}
}
public void TakeDamage (float amount)
{
health -= amount;
if (health <= 0)
{
FindObjectOfType <GameManager> ().EndGame ();
}
}
}
Game $$anonymous$$anager
using UnityEngine; using UnityEngine.Scene$$anonymous$$anagement; public class Game$$anonymous$$anager : $$anonymous$$onoBehaviour { bool gameHasEnded = false;
public float restartDelay = 1f;
public GameObject completeLevelUI;
public void CompleteLevel()
{
completeLevelUI.SetActive (true);
}
public void EndGame ()
{
if (gameHasEnded == false)
{
gameHasEnded = true;
Debug.Log ("GA$$anonymous$$E OVER");
Invoke ("Restart", restartDelay);
}
}
void Restart ()
{
Scene$$anonymous$$anager.LoadScene (Scene$$anonymous$$anager.GetActiveScene ().name);
}
}
using UnityEngine;
public class PlayerCollision : $$anonymous$$onoBehaviour { public Game$$anonymous$$anager game$$anonymous$$anager; public const float maxHealth = 100; public float currentHealth = maxHealth; void Start () {
}
void OnCollisionEnter (UnityEngine. Collision collisionInfo)
{
if (collisionInfo.gameObject.tag == "Obstacle")
{
float maxHealth = 100;
Debug.Log ("-100");
}
}
public void TakeDamage (float amount)
{
currentHealth -= amount;
if (currentHealth <= 0)
{
currentHealth = 0;
FindObjectOfType <Game$$anonymous$$anager> ().EndGame ();
}
}
}
Answer by Vicarian · May 18, 2018 at 05:08 AM
You have nothing calling TakeDamage in the scripts you've posted.
In your OnCollisionEnter event, where you check to see if the colliding object is "Obstacle", call TakeDamage(maxHealth); though your design probably isn't correct, so the collision event might not fire in the first place.
Your answer
![](https://koobas.hobune.stream/wayback/20220612162914im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to make a good hp system in unity 0 Answers
How to render UI Text in front of 3d gameObject on ScreenSpace Camera 0 Answers
Error cs0103: the name unityEngine don't exist in the current context 2 Answers
Unity 5 - How to duplicate enemies whilst making them unique? 1 Answer
Error initializing license system Please contact suppport @unity3d.com,Ошибка про запуске юнити 0 Answers