- Home /
Enemy spawn issues
Currently I'm attempting to spawn enemies based off of the 3 that I currently have, pictured below (pink, blue, red). I want them to spawn new clones 1 at a time over a period of time. I've already attempted at accomplishing this, but it doesn't seem to want to actually create the new enemies. What do I do? I'll post what I have so far for reference and see if I'm calling something wrong or if I'm doing it completely wrong.
#pragma strict
var pinkProjectile : Transform;
var blueProjectile : Transform;
var redProjectile : Transform;
//var pinkCopy : Transform;
//var blueCopy : Transform;
//var redCopy : Transform;
var enemy : Transform;
var shootCoolDown : float; //Wait 1.5 seconds before shooting again
var shootTimer : float; //Records how much time between shots
//var enemies : GameObject[];
var pinkLives = 1;
var blueLives = 2;
var redLives = 3;
var spawnEnemies = 0;
var spawn = false;
var timesHit = 0;
//var enemyArray = new Array();
//enemyArray.length = 3;
function Start () {
if(spawn == false) {
InvokeRepeating("spawnEnemy", 3, 5);
spawn = true;
}
}
function Update () {
shootLazer();
}
function OnTriggerEnter(collision:Collider) {
if(gameObject.name == "Enemy_Pink") {
if(collision.gameObject.tag == "Bullet") {
pinkLives--;
if(pinkLives == 0) {
Destroy(GameObject.Find("Enemy_Pink"));
}
}
}
if(gameObject.name == "Enemy_Blue") {
if(collision.gameObject.tag == "Bullet") {
blueLives--;
if(blueLives == 0) {
Destroy(GameObject.Find("Enemy_Blue"));
}
}
}
if(gameObject.name == "Enemy_Red") {
if(collision.gameObject.tag == "Bullet") {
redLives--;
if(redLives == 0) {
Destroy(GameObject.Find("Enemy_Red"));
}
}
}
Destroy(collision.gameObject);
}
function spawnEnemy() {
var instantiatedEnemy : Transform;
var newEnemyOriginX = enemy.transform.position.x;
var newEnemyOriginY = enemy.transform.position.y;
var newEnemyOriginZ = enemy.transform.position.z;
instantiatedEnemy = Instantiate(enemy, Vector3(newEnemyOriginX, newEnemyOriginY, newEnemyOriginZ), transform.rotation);
instantiatedEnemy.rigidbody.AddForce(instantiatedEnemy.transform.up * - 20);
spawnEnemies++;
}
Are you getting some error in the console? Is it spawning any enemy at all, or none?
All irrelevant code has been removed here: var enemy : Transform;
function Start () {
if(spawn == false) {
InvokeRepeating("spawnEnemy", 3, 5);
spawn = true;
}
}
function spawnEnemy() {
var instantiatedEnemy : Transform;
var newEnemyOriginX = enemy.transform.position.x;
var newEnemyOriginY = enemy.transform.position.y;
var newEnemyOriginZ = enemy.transform.position.z;
instantiatedEnemy = Instantiate(enemy, Vector3(newEnemyOriginX, newEnemyOriginY, newEnemyOriginZ), transform.rotation);
instantiatedEnemy.rigidbody.AddForce(instantiatedEnemy.transform.up * - 20);
spawnEnemies++;
}
And to answer your questions, I'm getting no errors in the console and it's spawning 0 enemies
Answer by suribe · Mar 08, 2014 at 11:46 PM
If with that code it's still not working, then spawn must be true for some reason. If you are using it only because you fear it might be called more than once, that should not happen if the script is used only in one gameObject in the scene. If you are using it in more than one, then you should redesign your game so this is centralized.
I would suggest adding a couple Debug lines in the Start function:
function Start () {
Debug.Log("spawn = " + spawn);
if(spawn == false) {
Debug.Log("Before InvokeRepeat");
InvokeRepeating("spawnEnemy", 3, 5);
Debug.Log("After InvokeRepeat");
spawn = true;
}
}
And also in spawnEnemy:
function spawnEnemy() {
var instantiatedEnemy : Transform;
var newEnemyOriginX = enemy.transform.position.x;
var newEnemyOriginY = enemy.transform.position.y;
var newEnemyOriginZ = enemy.transform.position.z;
Debug.Log("spawnEnemy");
instantiatedEnemy = Instantiate(enemy, Vector3(newEnemyOriginX, newEnemyOriginY, newEnemyOriginZ), transform.rotation);
instantiatedEnemy.rigidbody.AddForce(instantiatedEnemy.transform.up * - 20);
spawnEnemies++;
Debug.Log("spawnEnemies: " + spawnEnemies);
Debug.Log("Instantiated Enemy: " + instantiatedEnemy );
}
Or directly, try replacing your Start function with something that only invokes the repeating:
function Start () {
InvokeRepeating("spawnEnemy", 3, 5);
}
Your answer
Follow this Question
Related Questions
Stop spawning enemies when a bool is enabled 0 Answers
Spawn random amount apart of each other 1 Answer
Enemy spawn script not working properly ! 1 Answer
How to spawn enemies/objects that fly into a level 1 Answer
character moving after moving 0 Answers