Im trying to decrease health when a sphere collider enter another collider.
But im doing something wrong.
The actually bullet is a clone from the project folder.
1: When the bullet collider enter the object to kill collider nothing happens.
2: If i remove all health manually at runtime nothing happens.
3: If i manually check isDead bool the object doesnt gett destroyd
What is it im doing wrong?
CODE:
using UnityEngine;
using System.Collections;
public class EnemyHealth : MonoBehaviour
{
public int startingHealth = 100;
public int currentHealth;
public bool isDead;
void Awake ()
{
currentHealth = startingHealth;
}
void OnCollisionEnter(Collision col)
{
if(col.gameObject.name == "Bullet")
{
currentHealth = startingHealth - 10;
}
if (currentHealth <=0)
{
isDead = true;
Dead ();
}
}
void Dead ()
{
if (isDead = true)
{
Destroy (gameObject, 2f);
}
}
}
Hi cruising! Try 2 and 3 by hitting an enemy right after that! Theyre both in oncollisionenter meaning it wont be called unless you hit something.
Try using col.gameobject.tag instead of name.
Youd also have to make a new tag for that to work.
Also make sure enemy health is atached to a gameobject!
You need to call Dead() for it to check if it is dead. Just checking the bool will return true or false. Also is the collision even triggering?
Try adding a Debug.Log(“Entered”); to check if it even enters
Is the bullet gameobject defiantly named Bullet?
Do both objects have a rigidbody and a collider?
Also just so you know the currentHealth at the least in this case will only ever be 90. This is because when a collision enters you are assigning it the value startingHealth(100) - 10 = 90. You should decrement the current health instead.
I think this may solve your issue.
using UnityEngine;
using System.Collections;
public class EnemyHealth : MonoBehaviour
{
public int startingHealth = 100;
public int currentHealth;
public bool isDead;
void Awake ()
{
currentHealth = startingHealth;
}
void OnCollisionEnter(Collision col)
{
if(col.gameObject.name == "Bullet")
{
currentHealth = currentHealth - 10;
}
if (currentHealth <=0)
{
isDead = true;
Dead ();
}
}
void Dead ()
{
if (isDead = true)
{
Destroy (gameObject, 2f);
}
}
}
Yes if i did like you said it worked, i manually removed the health and checked isDead and made a shot, it got killed.
I laso changed from name to tag, idk if that made any bigger changes?
Thanks for your help!
Thanks for your tip there! i got it to work now, and actually, i had forgot to add a rigiboddy to the object to kill, silly mistake.
Thanks for your help!
First the if statement at line 33 needs to be “==” not “=”. that should give you a compiler error though. Second if the bullets are instantiated at runtime they are probably named “Bullet (clone)”. You can check this by running the game and watching the hierarchy when firing and see what the bullet’s name actually is. You can manually change the name when you spawn the bullet.
I would diffidently check if the collision is happening though. Adding the Debug line will help just make sure to remove when you solve your problem.
Another Question quick. How are you despawning the bullet?? If the bullet is being destroyed on collision that could be happening before the player can do it’s method and then the name becomes “missing” because the gameObject is null but the program knows something is there. I would consider the following on the enemy.
public void TakeDamage(int damage)
{
currentHealth -= damage;
if (currentHealth <= 0)
Dead();
}
this way if the object that hits the bullet hits is an enemy and can take damage it will. if it is something else like a wall the bullet is then destroyed. Also if you are doing the enemy’s collisions you are calling that every few fame to see if the floor he is walking on is named “Bullet”. It is minor but with a horde game (Left 4 Dead, Diablo) this adds up every fixed update. Also if you are not destroying the bullets they are somewhere taking up ram and physics power.