Hi All,
I have been using unity for about 2 weeks now and I’m fairly new to programming as well so please excuse my ignorance. I am trying to build a simple spawn mechanic that will spawn objects and send them towards the player. It appears to be working but I have noticed that sometimes there will be 2 objects in exactly the same place. This wouldn’t normally be a problem but it is causing my die() method to be triggered twice which causes life counting problems, here is my spawn script any help would be much appreciated. If you need any more information I will do my best to provide it.
#pragma strict
var block1 : GameObject;
var scriptJunky: Transform;
var rand: int;
var xSpawn: int;
var xSpawnOld: int;
var k :int;
var xSpawns = new Array();
var xSpawnsFloat: float[];
var x: int = 0;
var clone : GameObject;
var startblock: GameObject;
function Start () {
xSpawnsFloat = [-2,-1.5,-1,-0.5,0,0.5,1,1.5,2];
InvokeRepeating("SpawnBlockTimer", 0, 0.6);
xSpawnOld = 1;
Random.seed = System.DateTime.Now.Second;
}
function Update(){
xSpawn = Random.Range(0,9);
}
function SpawnBlockTimer() {
var restartScript: RestartGUI = scriptJunky.GetComponent("RestartGUI") as RestartGUI;
if(restartScript.isPaused == false){
if(xSpawn < xSpawnOld){
xSpawnOld = xSpawn + 3;
SpawnBlock();
Debug.Log("SpawnBlock < Triggered" + System.DateTime.Now);
}else if (xSpawn > xSpawnOld){
xSpawnOld = xSpawn - 3;
SpawnBlock();
Debug.Log("SpawnBlock > Triggered" + System.DateTime.Now);
}else if (xSpawn == xSpawnOld){
xSpawn = Random.Range(0,9);
}
}
}
function SpawnBlock(){
clone = Instantiate(block1, Vector3(xSpawnsFloat[xSpawnOld],0.45,20), block1.transform.rotation);
//Render it visable
var mesh : MeshRenderer = clone.GetComponent(MeshRenderer);
mesh.enabled = true;
//Destroy after x seconds
Destroy (clone, 3);
if(clone.activeSelf == true){
clone.transform.tag = "Enemy";
}
var enemies = GameObject.FindGameObjectsWithTag("Enemy");
//Check how many obsticles are alive
if(enemies.Length > 8){
Destroy(clone);
}
yield WaitForSeconds(0.5);
}
Thank you in advance!!!