- Home /
bullets that do damage
I have a code here (C#) so that when my bullet collides with my enemy the enemy looses life and the bullet gets destroyed, but it wouldn't work, please help
using UnityEngine; using System.Collections;
public class EnemyHealth : MonoBehaviour { public int maxHealth = 100; public int curHealth = 100;
// Use this for initialization void Start () { } // Update is called once per frame void Update () { if (curHealth <1){ Destroy(gameObject); } } void OnCollisionEnter(Collision col){ if (col.gameObject.tag == "Bullet"){ curHealth -= 20; Destroy(gameObject.tag )("Bullet"); } } void OnGUI() { GUI.Box(new Rect(10,40, Screen.width / 2 / (maxHealth / curHealth), 20), curHealth + "/" + maxHealth); } }
I tagged my bullet as: Bullet
Try to format your code before you post it, it's hard to read that without proper formatting. Just click on the little 1's and 0's and then copy/paste the code in the box that pops up and press O$$anonymous$$. It would be great if you could edit your post and do that for everyone who is trying to help you, thanks.
Also you were a bit vague, when you say please help, what do you us to help you with. Tell us what you want in the end and we can provide you with a better answer... Vague questions get vague responses. Take your time, don't rush, no one is making you hurry.
Answer by azmat786n · Dec 01, 2012 at 06:29 AM
using UnityEngine;
using System.Collections;
public class EnemyHealth : MonoBehaviour {
public int maxHealth = 100;
public int curHealth = 100;
//initialization
void Start () { }
// Update
void Update () {
if (curHealth <1){
Destroy(gameObject);
}
}
void OnCollisionEnter(Collision col) {
if (col.gameObject.tag == "Bullet"){
curHealth -= 20;
Destroy(col.other);
}
}
Your script doesn't seem to work. It keeps giving me a parsing error on the last line.
void OnCollisionEnter(Collision col) {
if (col.gameObject.tag == "Bullet") {
curHealth -= 20;
Destroy(col.other);
}
}
<--- Right here we need a closing bracket '}'
Your answer
Follow this Question
Related Questions
Health System Raycast Bullets 2 Answers
Health Regeneration 2 Answers
how to make enemies have health and get damaged by bullets 3 Answers
Enemy Health, Player Damage 0 Answers
Damage/Health problem 2 Answers