- Home /
Where is the problem with the script, i want my player to die when his health is <1
Assets\Health.cs(43,21): error CS0119: 'GameObject' is a type, which is not valid in the given context
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class Health : MonoBehaviour { public int health; public int numOfHearts;
public Image[] hearts;
public Sprite fullHeart;
public Sprite emptyHeart;
void Update(){
if (health > numOfHearts)
{
health = numOfHearts;
}
for (int i = 0; i < hearts.Length; i++)
{
if (i < health)
{
hearts[i].sprite = fullHeart;
}
else
{
hearts[i].sprite = emptyHeart;
}
if (i < numOfHearts)
{
hearts[i].enabled = true;
}
else
{
hearts[i].enabled = false;
}
}
if (health < 1)
{
Destroy(GameObject);
}
}
}
Answer by Larry-Dietz · Dec 05, 2019 at 05:54 PM
Change your destroy line to Destroy(gameObject); (Lower case g)
Assuming your health is being decremented elsewhere in your script, once it drops below 1 the gameobject this script is attached to should be destroyed.
Using the uppercase G like you have, it is passing an object type to destroy, instead of the actual gameobject the script is attached to.
Hope this helps, -Larry
thanks, it worked, i'm still learning coding and i don t know that much
Your answer
Follow this Question
Related Questions
GameObject is already being activated or desactivated 2 Answers
Text component change through script not working 1 Answer
Timer for destroying/ appearing Gameobject (Vuforia) 2 Answers
Disabling A Script on a GameObject From a Different Script 2 Answers
What does "Couldn't find matching instance in prefab" mean? 2 Answers