- Home /
Spawning objects on trigger does not work as expected
Hello everyone! I created an enemy spawning system, that does not work as it should. Here are the scripts.
Spawner
#pragma strict
function OnTriggerEnter(collision : Collider) {
if(collision.CompareTag("Player"))
{
print("YAY");
EnemySpawn.SpawnEnemy = true;
}
}
EnemySpawn
#pragma strict
var prefab : Rigidbody;
static var SpawnEnemy = false;
function Update () {
if(SpawnEnemy == true){
print("Enemy has spawned");
Instantiate(prefab,transform.position,transform.rotation);
SpawnEnemy = false;
}
}
Those scripts should spawn 3 cubes(enemys) in 3 Empty GameObject positions (EnemySpawn1, EnemySpawn2, EnemySpawn3) with "EnemySpawn" script attached when a player enters a trigger. But the "EnemySpawn" script only spawns 1 cube(enemy) in 1 Empty GameObject position (EnemySpawn1) what did I do wrong? Thank you for help.
Answer by Flynn · Jan 18, 2013 at 08:33 PM
Hello, there! First of all, I suggest that instead of using boolean signals to tell SpawnEnemy to spawn, that you instead go a little more directly: ###EnemySpawn.js:
#pragma strict
var prefab : Rigidbody;
public function SpawnEnemy()//Create a public function we can call later
{
print("Enemy has spawned");
Instantiate(prefab,transform.position,transform.rotation);
}
Spawner.js
public var eSpawn : EnemySpawn;
#pragma strict
function OnTriggerEnter(collision : Collider)
{
if(collision.CompareTag("Player"))
{
print("YAY");
eSpawn.SpawnEnemy();//Call the function we defined earlier
}
}
But besides all of that, to actually answer your question, it seems that it only spawns one, because you only ever tell it to! Does your prefab contain more than one object in it? If not, I suggest doing this:
EnemySpawn.js
#pragma strict
var prefab : Rigidbody;
public function SpawnEnemy()
{
for (int i = 0; i < 3; i++)//Create a for loop that repeats its self 3 times
{
print("Enemy has spawned");
Instantiate(prefab,transform.position,transform.rotation);
}
}
Hmmm... Now it gives me an error message Assets/Scripts/Spawner.js(8,28): BCE0020: An instance of type 'EnemySpawn' is required to access non static member 'SpawnEnemy'. what do I do?
OH! I did not notice you were using static references... I have modified my code (added static keywords)
I tried adding static keywords before and then this showed up Assets/Scripts/EnemySpawn.js(8,13): BCE0020: An instance of type 'EnemySpawn' is required to access non static member 'prefab'. This is so confusing...
Very sorry, it's been a weird day. I've changed both scripts -- Be sure to, in the editor, set the "eSpawn" variable to the object that has the EnemySpawn script on it
Great it works! I just had to change the Spawner script a bit. I added 3 "eSpawn" variables, like this.
public var eSpawn1 : EnemySpawn; public var eSpawn2 : EnemySpawn; public var eSpawn3 : EnemySpawn;
function OnTriggerEnter(collision : Collider) { if(collision.CompareTag("Player")) { print("YAY"); eSpawn1.SpawnEnemy();//Call the function we defined earlier eSpawn2.SpawnEnemy(); eSpawn3.SpawnEnemy(); } }
Thanks for the help!
Answer by GS-Kiwibird · Jan 18, 2013 at 10:44 PM
I believe the issue is that your EnemySpawn1 instantiates an enemy, and then sets the static variable "SpawnEnemy" to false. Since it is a static variable, and that variable is now false, the other EnemySpawners will not spawn any enemies.
Try this:
Spawner
#pragma strict
function OnTriggerEnter(collision : Collider)
{
if(collision.CompareTag("Player"))
{
print("YAY");
var enemySpawners : GameObject[] = GameObject.FindGameObjectsWithTag("EnemySpawner");
for(var enemySpawn : GameObject in enemySpawners)
{
enemySpawn.BroadcastMessage("SpawnEnemy");
}
}
}
EnemySpawn
#pragma strict
var enemyPrefab : GameObject;
function SpawnEnemy()
{
print("Enemy has spawned");
Instantiate(enemyPrefab, transform.position, transform.rotation);
}
Make sure to your EnemySpawners are tagged as "EnemySpawner".
This works fine too! Glad to see a different way of fixing this. Your script looks more complicated for me, but I think it has a better way to spawn the enemies. Thank you.