- Home /
Spawning 2 objects in exactly the same place!!!
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!!!!
Answer by Tehnique · Jul 23, 2014 at 03:20 PM
Well, there are multiple solutions:
Add a collider (& rigidbody) to your objects to stop them from intersecting eachother. This might give weird results if 2 are spawned in the same place.
Remember where you spawned your last object, and if the new position is the same, get a new position.
The one I'd recommend: Fix your "die" method, so after an object hits your player, you set 1-2 seconds for the player to be invulnerable, so even if a second enemy hits him, life counters don't go down. You can just use a bool and the Invoke method for this (have a bool like "isRespawning", set it to true on the first hit and call an Invoke method that sets it to false after 2 sec. When you are hit, check that "isSpawning" is false, if it is true, do nothing). I don't know what game you are building, but lots of games have this mechanic.
Thank you very much, I have just tried your recommended fix and it appears to have worked perfectly. Just out of curiosity, do you have any idea why it might be spawning duplicates? as my spawn method should only be called every 0.6seconds anyway? Thank you again!
Well, your method is not called every 0.6 seconds, it just takes 0.6 seconds to execute, it can be called 100 times, and 0.6 seocconds later you will have 100 spawns. Take another look at coroutines, use a IEnumerator return type on your spawn method, use a yield return statement (an example here), and most importantly, set a bool, like "isSpawning" that is set to true when you enter the method, and false after your yield statement in the method. From the outside, call the Spawn method ONLY if "isSpawning" is false.
This should fix it.
Also, if you do what is described in this comment, there is no need for the Invoke and isSpawning bool from the answer. You chose between the 2 solutions (the one in this comment would be cleaner imo).