- Home /
When the original enemy dies, the spawns stop (javascript)
Ok I'm not sure what is going on here. Although I am fairly new to this stuff, so thats most likely the problem. So this is how my project is set up right now. I have a Game Object Cube with tag "enemy" that acts as the enemy, its script is to chase after the player. I also have a Game Object Empty with tag "spawnpoint" that spawns copies of the enemy object.
The problem is that when I kill the original object is stops spawning the enemies from the spawn points and I get a MissingReference Transform Destroyed error. It also happens when if I shot the enemy (it takes 2 shots to kill) and then leave it alone all the spawns only have to be shot once to be killed. I initially thought that all I had to do was create an enemy prefab, but that doesn't work because one of the enemies variables is for searching for the player GameObject, so I can't (more like don't know how) to pass that into the prefab.
So I started thinking it may just be the code that needs to be changed, but I'm not exactly sure what to change to get it to work. Below are my EnemyScript and SpawnScript.
EnemyScript
var player : GameObject;
var speed : float = .5;
public var enemyHealth : int = 2;
function Start()
{
player = GameObject.FindGameObjectWithTag("Player");
//if(!player)
//Debug.Log("Could Not Find The Player");
}
function Update()
{
if(!player)
return;
var distance = Vector3.Distance(player.transform.position, transform.position);
if(distance < 100)
{
//Debug.Log("Player is close by");
var delta = player.transform.position - transform.position;
delta.Normalize();
var moveSpeed = speed * Time.deltaTime;
transform.position = transform.position + (delta * moveSpeed);
}
else
{
//Debug.Log("Not close enough" + distance);
}
if(enemyHealth <= 0)
{
Level1InfoScript.playerScore += 100;
Destroy(gameObject);
}
}
function OnCollisionEnter(otherObject : Collision)
{
if(otherObject.gameObject.tag == "bullet")
{
enemyHealth --;
}
}
SpawnScript
var enemyPrefab: Transform;
var maxEnemiesAlive: int = 1;
var maxEnemies: int = 30;
private var totalEnemies: int;
private var spawnPoints: GameObject[];
private var player: Transform;
var fNextSpawn : float = 0.0;
var fSpawnInterval : float = 5.0;
var iMaxEnemies : int = 10;
var iTotalEnemies : int = 0;
function Start()
{
fNextSpawn = Time.time + fSpawnInterval;
spawnPoints = GameObject.FindGameObjectsWithTag("spawnpoint");
player = GameObject.FindWithTag("Player").transform;
totalEnemies = maxEnemies; // define how many enemies the level has
}
function Update()
{
if(Time.time > fNextSpawn && iTotalEnemies < iMaxEnemies)
{
fNextSpawn = Time.time + fSpawnInterval;
spawnEnemy();
}
// when some enemy is killed and total enemies isn't zero yet...
if (transform.childCount < maxEnemiesAlive && totalEnemies > 0)
{
// draw a random spawn point:
var pos = spawnPoints[Random.Range(0, spawnPoints.length)].transform.position;
// find a rotation to look at the player:
var rot = Quaternion.LookRotation(player.position - pos);
// create the new enemy facing the player:
var enemy = Instantiate(enemyPrefab, pos, rot);
// child it to Enemies (this object):
enemy.parent = transform;
totalEnemies--; // decrement the remaining enemies
}
}
function spawnEnemy()
{
Instantiate(enemyPrefab, transform.position, Quaternion.identity);
iTotalEnemies++;
}
Answer by vinod.kapoor · Dec 03, 2012 at 07:58 AM
There are two ways as far as I know : First one is to keep the enemy in the hierarchy and de active him by unticking the button in the inspector and then passing it as a variable in the spawn point script as a enemy prefab transform. it will now instantiate the clone of enemy.
this thing has a drawback since the enemy will be loaded at the time of level load and will stay in the game till the level end.
the other way is to make the prefab just like you did earlier and the pass the Player gameObject by writing this in Start function as:
player = GameObject.Find("player name in the heirachy ").transform;
this is better then the previous technique
well the code you provided didn't work, I kept getting an error saying "cannot convert UnityEngine.Transform to UnityEngine.GameObject"
However the other part you mentioned did help me out. I created a prefab of the player object, then passed the player prefab into the enemy prefab, then the enemy prefab into the spawn point scene object and deleted the original enemy scene object. So far the only problem I have encountered is that after awhile (and killing all the others) only 1 enemy will be on screen at a time and another wont appear until that one is dead. I thought it was because one of the variables says maxenemiesalive = 1, but I changed it to 5 and it didn't seem to change anything. Other than that, everything is working great. So thanks.
Answer by vinod.kapoor · Dec 04, 2012 at 05:19 AM
you should have taken the player as transform. then the code would have worked. see the player is currently a GameObject . for this you should have used
//if the player is declared as a GameObject
player = GameObject.Find("player name in the heirachy ")
//if the player is declared as transform player = GameObject.Find("player name in the heirachy ").transform;
Your answer
Follow this Question
Related Questions
Load Prefabs in Array with Javascript 1 Answer
Bullet Instantiation 1 Answer
How would I change the value of one prefab's variable via script without changing all of them? 1 Answer
Prefab problem, wont accept gameObject in var 3 Answers
Prints correct on console but not updating on Prefab/Gameobject. 2 Answers