- Home /
When I kill one enemy, the rest disapear, but still shoot
So I have made my enemy a prefab and when I enter a invisible trigger box, it starts to spawn them. The only problem is that when I shoot one the rest disappear, but I can still shoot them even though they're invisible now, and they'll still shoot back. I'm also getting the following error 3 times as soon as I run the game: NullReferenceException: Object reference not set to an instance of an object. Any help on how to solve this problem would be most welcomed.
Here's my code for spawning the enemies(Attached to the spawning area):
var Enemy: Transform; var SpawnEnemies: int = 0;
var spawn = false;
function OnTriggerEnter(other: Collider){
if(other.tag == "Player" && spawn == false)
{
InvokeRepeating ("Spawn",1,1);
spawn = true;
}
}
function Spawn(){
var instantiatedenemy = Instantiate(Enemy,GameObject.Find("EnemySpawn").transform.position, transform.rotation);
SpawnEnemies +=1;
if(SpawnEnemies >=10)
{
CancelInvoke("Spawn");
spawn = false;
}}
My code for when the enemy is destroyed is (attached to enemy):
if(life <= 0)
{
Destroy(gameObject);
}
Code for when a bullet hits the enemy (attached to bullet):
function OnTriggerEnter(enemy : Collider){
if(enemy.tag == "Enemy")
{
Destroy(gameObject);
EnemyScript.life -=1;
}
}
Yep, going to need the code alright :)
Better post the code that makes an enemy disappear to start with - probably the spawning code too.
I'll edit the question to include it. Thank you for letting me know which code would be needed.
Answer by whydoidoit · May 28, 2012 at 07:59 PM
EnemyScript.life is a static isn't it? Got to be that - you need it to be a normal variable, otherwise they all share it.
public var life : float;
Then in OnTriggerEnemy:
if(enemy.tag == "Enemy") {
Destroy(gameObject);
enemy.GetComponent(EnemyScript).life -= 1;
}
That worked perfectly. Thank you very much. This doesn't have to do with the original question, but I'm sure it's an easy fix, and I don't want to start a new question for the second time in 3 hours. When my enemies shoot, they all shoot from on spawn point of one enemy, when they should all be shooting from their own spawnpoints attached to them. This is my shooting code:
allowfire = false; var instantiatedbullet = Instantiate(bullet,GameObject.Find("EnemyGunSpawn").transform.position, Quaternion.Euler(direction.normalized)); yield WaitForSeconds(0.75); allowfire = true;
EnemyGunSpawn is where the bullets shoot from, and the direction is towards the player. I know that they all shoot from the first spawned enemies EnemyGunSpawn, but I'm not sure how to have it so they shoot from their own. Thank you for any help on this and all the help you've given.
Actually, do start a new question - we can get the formatting right then and I'll be able to read it :)
Your answer
Follow this Question
Related Questions
Scripting Error Help Needed 2 Answers
Error BCE0044 JavaScript 1 Answer
Script copied exactly, yet I get the console errors 1 Answer
Incremental game need help 1 Answer
Unknown identifier "If" (Javascript) 2 Answers