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++;
}