- Home /
Enemy health and bullet problem
[Edit]: This script is attached to the enemy, by the way!
Hello, I'm having a little trouble with my enemy health script. I asked a question similar to this one a while ago, but I'm using my newer script now, and can't figure out how to solve this problem.
Here's the health script:
var hit = 0;
var bulletHit : AudioSource;
var bullet : GameObject;
function OnTriggerEnter(){
hit +=1;
Update();
audio.Play();
}
function Update(){
if(hit == 3){
Destroy(bullet.gameObject);
Destroy(gameObject);
}
}
It basically makes it so that if the bullet object enters the trigger on the enemy (Collider set to trigger) it adds a hit to it and if there are 3 hits, it destroys it.
The Problem: The bullets don't get destroyed when they hit the enemy. They continue to travel for 5 seconds (because of the Timed Object Destructor.) I tried to use this, but it didn't work either:
DestroyImmediate(bullet, true);
and
DestroyImmediate(bullet.gameObject, true);
Can someone help me with this? Thanks.
What if you tried destroying the bullet in the OnTriggerEnter function? Isn't that when they hit the enemy?
Destroying GameObjects immediately is not permitted during physics trigger/contact, animation event callbacks or OnValidate. You must use Destroy ins$$anonymous$$d. UnityEngine.Object:DestroyImmediate(Object, Boolean) ThreeHitDeath:OnTriggerEnter() (at Assets/ThreeHitDeath.js:6)
and when I use Destroy:
Destroying assets is not permitted to avoid data loss. If you really want to remove an asset use DestroyImmediate (theObject, true); UnityEngine.Object:Destroy(Object) ThreeHitDeath:OnTriggerEnter() (at Assets/ThreeHitDeath.js:6)
[Edit] Still trying to fix this, but I keep getting these errors, does anyone know how to fix this?
Answer by santosh810666 · Dec 08, 2013 at 05:54 AM
try this
var hit = 0;
var bulletHit : AudioSource;
var bullet : GameObject;
function OnTriggerEnter(){
hit +=1;
Update();
audio.Play();
}
function start(){
bullet = GameObject.Find("Name of your bullet");
}
function Update(){
if(hit == 3){
Destroy(bullet);
Destroy(gameObject);
}
}
Didn't seem to work, I'll try using tags
[Edit] Tags did not work either. I did:
var hit = 0;
var bulletHit : AudioSource;
var bullet : GameObject;
function OnTriggerEnter(){
hit +=1;
Update();
audio.Play();
}
function Update(){
if(hit == 3){
Destroy(gameObject);
Destroy(GameObject.FindWithTag("Bullet"));
}
}
try this
var hit = 0;
var bulletHit : AudioSource;
var bullet : GameObject;
function OnTriggerEnter(){
hit +=1;
Update();
audio.Play();
}
function start(){
bullet = GameObject.Find("Name of your bullet");
}
function Update(){
if(hit.bullet.tag == "Enemy"){
Destroy(bullet);
Destroy(gameObject);
}
in your code your saying that if you hit 3 times to enemy then the bullet should destroy.It is not correct if you want to destroy bullet immediate,say that if your bullet hit enemy then destroy bullet and enemy
$$anonymous$$issingFieldException: System.Int32.bullet
Answer by Spinnernicholas · Dec 08, 2013 at 07:03 AM
Create a boolean to represent if the bullet is destroyed. Initialize it to false. When the collision occurs, set it to true. Then, in the Bullets Update(), you can call destroy immediate.
I haven't used booleans yet, I've researched them a bit, but I don't know how to use them. I've created at the top of my script:
var isBulletDestroyed : boolean = false;
Now from there on I'm not sure which way to go.
When bullet is destroyed:
isBulletDestroyed = true;
then, in Update:
if(isBulletDestroyed)
{
DestoyImmediate(this);
}
All logical operations(==,!=,<,>,....) return boolean values.
Not working, it seems to think I'm actually trying to destroy the bullet prefab itself, not the bullet that was instantiated when fired. I tried DestroyImmediate and Destroy, they both tell me to try the opposite of each other.