- Home /
Enemy's wont die if spawned
I have some simple scripts which destroys my enemy when my bullet collides with them, this works fine if I start the game with the enemy's already spawned whereas if the enemy's are spawned when my game is playing via the spawn script at the bottom of the page they dont destroy. I dont understand why this happens can anyone help me out?
Spawn script,
using UnityEngine;
using System.Collections;
public class Spawn : MonoBehaviour
{
// Spawn location
public Vector3 spawnLocation = Vector3.zero;
// Spawn radius (Gives a bit more randomness factor to the spawn location)
public float spawnRadius = 1.0f;
// Spawn timer (seconds)
public float spawnTimer = 5.0f;
private float spawnTimeRemaining = 5.0f;
// The zombie to spawn
public GameObject zombiePrefab = null;
void Awake()
{
spawnTimeRemaining = spawnTimer;
}
void FixedUpdate()
{
spawnTimeRemaining -= Time.deltaTime;
if (spawnTimeRemaining < 0.0f)
{
Vector2 circlePosition = Random.insideUnitCircle * spawnRadius;
GameObject.Instantiate(zombiePrefab, spawnLocation + new Vector3(circlePosition.x, 0.0f, circlePosition.y), Quaternion.identity);
spawnTimeRemaining = spawnTimer;
}
}
}
These are the two scripts on my bullets,
var bloodSplat : GameObject;
var explosion : GameObject;
function OnCollisionEnter (hit:Collision){
if(hit.collider.tag == "Enemy"){
var expl = Instantiate(bloodSplat, transform.position, Quaternion.identity);
Destroy(gameObject);
}
else
var bloo = Instantiate(explosion, transform.position, Quaternion.identity);
Destroy(gameObject);
}
function OnCollisionEnter(myCol: Collision){
if(myCol.gameObject.name == "Enemy"){
Destroy(myCol.gameObject);
}
}
We'd need to see the collisions part to understand better. Also I'm pretty sure your code formatting isn't right.
$$anonymous$$aybe the spawns are not named or tagged properly after they are spawned. Pause and take a look on that.
The enemy's are still tagged as Enemy once they are spawned.
Answer by mesho · Dec 30, 2011 at 08:23 PM
i found one error now , on the last function you had to shift the (IF Statement Answer ) by pressing Tab , it may fix that because the code will not be executed correctly if it has a good syntax and wrong little things it must be :
function OnCollisionEnter(myCol: Collision){
if(myCol.gameObject.name == "Enemy"){
Destroy(myCol.gameObject);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613062504im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Destroy Scripting Help 1 Answer
Destroy enemy using raycasts 2 Answers
Need help with enemy spawner 2 Answers
Animation interference problem 1 Answer
A node in a childnode? 1 Answer