- Home /
The question is answered, right answer was accepted
Bullet subtracting damage from enemy health
So like I've spent the last hour searching around for how to fix this, and dispite writing down some peoples codes word for word character for character I can not figure this out.
So basically I have a bullet script, that makes the object move forward based on the objects public float, and a damage int to be applied to enemies.
public class bullet : MonoBehaviour {
public int damage;
public float velocity;
void Update () {
// bullet movement
transform.position += transform.right * Time.deltaTime * velocity;
}
// bullet dealing damage, then destroying itself
void OnCollisionEnter2D(Collision2D coll) {
if (coll.gameObject.tag == "Enemy")
coll.gameObject.GetComponent<EnemyHealth>().Health = Health - damage;
Destroy (gameObject);
}
}
it then in theory applies this damage effect to my enemy script:
public class EnemyHealth : MonoBehaviour {
public int Health;
void Update () {
if (Health => 0f)
{
Destroy (gameObject);
}
}
}
The bullet moves correctly and disapears on hit, but when I have the code to adjust the enemies health I get the error: EnemyHealth.cs(10,7) error CS1660: Cannot convert 'lambda' to non-delegate type 'bool'
There isn't a single bool anywhere in this code so I have no idea what it's talking about.
I also get the error: bullet.cs(17,57): error CS0103: The name 'Health' does not exist in the current context
so yeah I'm at a loss, any ideas?
Edit: Side note, I fixed the error "EnemyHealth.cs(10,7) error CS1660: Cannot convert 'lambda' to non-delegate type 'bool'" simply by changing the "health <= 0" to health < 1" It basically works the same since it's an int not a float, so that problem is fixed at least.
This line is producing your error
coll.gameObject.GetComponent<EnemyHealth>().Health = Health - damage;
That single Health doesn't exist.
change the line to this
coll.gameObject.GetComponent<EnemyHealth>().Health -= damage;
Answer by LiamofElites · Feb 14, 2018 at 12:57 AM
This can be made very easy thanks to the API SendMessage. I created a game like this too, so this should do it
bullet.cs
public int damage;
public float velocity;
void Update () {
// bullet movement
transform.position += transform.right * Time.deltaTime * velocity;
}
// bullet dealing damage, then destroying itself
void OnCollisionEnter2D(Collision2D coll) {
if (coll.gameObject.tag == "Enemy")
coll.gameObject.SendMessage("hit", damage);
Destroy (gameObject);
}
EnemyHealth.cs
public int Health;
void hit(int damage){
Health -= damage;
Debug.Log("Hit");
}
So I made the mentioned changes, and things are at least working better, it runs without giving me an error, but the bullet still isn't dealing damage at all, I have the enemies health set to 5 and bullet damage set to 2 and no changes :/
(also side not I realised i'm dumb because I made the enemy die when health was above 1, not lower, so I fixed that)
thanks for the assistance so far though! nice to make some progress after like an hour of nothing
No problem! and have you tried declaring the health and damage in the awake method? I think that might work but if not you should post that part of the code and I'll take a look at it.
Not quit sure what you mean there, I don't have an awake section of that code yet. would I put public int health and damage in the void Awake? Sorry I'm still learning.
Follow this Question
Related Questions
Health and Damage [C#] 2 Answers
My bullet script doens't work. 1 Answer
bullet damage problem 1 Answer
Health Regeneration 2 Answers
Health bar losing health 1 Answer