Why isn't myenemy instantiating?

Hi everyone,

I have my spawn enemy script attached to the enemy spawnpoint, the enemy is a prefab, here’s my script.

var enemy : Transform;

//InvokeRepeating("spawnEnemy",1,1);
function spawnEnemy() {

Instantiate(enemy, transform.Find ("enemyspawn1").transform.position,transform.rotation);
}

I did remember to drag the enemy prefab into the enemy Transform. When the script is on the enemyspawn1 object I get no error message but nothing happens, and if I put the script on the enemy prefab I get “Null reference exception”

I’m pretty stumped on this one, I’d love a bit of help, thanks.

Is spawnEnemy() actually getting called anywhere? Also, are you sure transform.Find() is returning a valid transform? And are you sure you want to be using Transform.Find() at all? (Or is it maybe just transform.position that you want?)

Transform.Find() returns a transform, I believe, so I think you’d want:

transform.Find ("enemyspawn1").position

Rather than:

transform.Find ("enemyspawn1").transform.position

(Although now that I think about it, I guess the latter will work as well.)

thanks for the post,
I took the // of of the InvokeRepeating, so it should be getting called there. Yes, I’m sure that transform.Find() has a proper transform, no I’m not sure if transform.Find is what I want to be using, as far as I know it’s the only way to reference the position of that certain object.

Is the object you’re trying to reference the same as the object the script is attached to? (Also, Transform.Find() is never the only way to reference an object, although sometimes it’s an appropriate way.)

Oh, I didn’t think about about the fact that it was attached to that object. The enemy is still not instantiating however, here’s the current code

var enemy : Transform;
function Start () {

InvokeRepeating(("spawnEnemy"),1,1);
}
function spawnEnemy() {

Instantiate(enemy, transform.position,transform.rotation);

}

hmmm…where is function spawnEnemy() called? you need to call the coroutine from somewhere…

for example:

var enemy : Transform;
function Start () {
spawnEnemy(); //  :smile: have fun watching 1000 enemies spawning ;)
//InvokeRepeating(("spawnEnemy"),1,1);
}
function spawnEnemy() {

Instantiate(enemy, transform.position,transform.rotation);

}

or perhaps like that:

var EnemySpawn : GameObject;
var EnemyCount:int = 4;

var PlayerSpawn: GameObject;
private var PlayerCount:int = 1;


function Start(){
//Spawns player prefab
	for (var e=0;e<PlayerCount;e++) {
		Instantiate (PlayerSpawn, Vector3(0,0,0), Quaternion.identity);
	}
//Spawns enemie prefabs
	SpawnEnemies();
}
	SpawnEnemies();
}
function SpawnEnemies(){
	yield WaitForSeconds(2);
	for (var i=0;i<EnemyCount;i++) {
		Instantiate (EnemySpawn, Vector3(Random.value * 250.0+EnemyCount, Random.value *250.0+EnemyCount, Random.value *250.0+EnemyCount), Quaternion.identity);
	}
}

another possibility:

…bla vars

//...
function  SpawnEnemies() {
	yield WaitForSeconds(2);
	for (var i=0;i<EnemyCount;i++) {
		Instantiate (EnemySpawn, Vector3(Random.value * 250.0+EnemyCount, Random.value *250.0+EnemyCount, Random.value *250.0+EnemyCount), Quaternion.identity);
	}
}
function OnGUI()
{
	if(GUI.Button(Rect(20, 20, 120, 25),"SpawnEnemies"))
		SpawnEnemies();
}

…all 3 exapmles CALL the SpawnEnemies() function… without calling it no enemy will spawn.

This is a basic timed spawn. The first thing I asked was SpawnEnemy called. This kind of calls it for you.

var enemy : Transform;
var spawnRate=5.0;
private var nextSpawn=0.0;//Make this number larger to not spawn immediately
function Upate() {
if(Time.time<nextSpawn)return;
nextSpawn=Time.time + spawnRate;
Instantiate(enemy, transform.position,transform.rotation);
}

@PressAnyKey + BigMisterB: InvokeRepeating is a form of Coroutine, This means that when he calls InvokeRepeating at the Start of the scripts it will continuously call the spawnEnemy function until it is told to stop, at the given interval he gave it.

@Wes: The last snippet of code you posted works as intended. If there is an issue that it is still not spawning, then the issue is somewhere else. To test it to be sure I am right, simply create a new scene, create an empty game object, attach the script, and drag in your enemy prefab to the variable and hit play. It will generate as intended.

Thanks everyone(Especially Rob), in the end it was indeed something wrong, all I had to do was remove the script and re-apply it to the spawnpoint, talk about frustrating.
thanks again Rob.

@Ezzerland yo i see