I have two scripts that randomly spawn models in an area and then respawn ones the overlap. I can’t find any errors in the code, but when Destroy(gameObject); is called in the second script, it seems to do nothing. Here is the base spawning script:
using UnityEngine;
using System.Collections;
public class CreateBuildings : MonoBehaviour {
public GameObject ThreeSBWR;
public GameObject ThreeSB;
public GameObject TwoSBWR;
public GameObject TwoSB;
public GameObject OneSBWR;
public GameObject OneSB;
public int ThreeSBTS;
public int TwoSBTS;
public int OneSBTS;
Quaternion SpawnRotation;
void Start ()
{
int a;
int b;
int c;
a = ThreeSBTS;
while (a > 0)
{
Vector3 SpawnLocation;
SpawnLocation = new Vector3(Random.Range(-90, 90), 2.5f, Random.Range(-90, 90));
while (SpawnLocation.x < 15 && SpawnLocation.x > -15 && SpawnLocation.z < 15 && SpawnLocation.z > -15)
{
SpawnLocation = new Vector3(Random.Range(-90, 90), 2.5f, Random.Range(-90, 90));
}
SpawnRotation.eulerAngles = new Vector3(0, Random.Range(0, 360), 0);
float rand;
rand = Random.Range(0, 1);
if (rand >= 0.5f)
{
Instantiate(ThreeSB, SpawnLocation, SpawnRotation);
}
else
{
Instantiate(ThreeSBWR, SpawnLocation, SpawnRotation);
}
a -= 1;
}
b = TwoSBTS;
while (b > 0)
{
Vector3 SpawnLocation;
SpawnLocation = new Vector3(Random.Range(-90, 90), 2.5f, Random.Range(-90, 90));
while (SpawnLocation.x < 15 && SpawnLocation.x > -15 && SpawnLocation.z < 15 && SpawnLocation.z > -15)
{
SpawnLocation = new Vector3(Random.Range(-90, 90), 2.5f, Random.Range(-90, 90));
}
SpawnRotation.eulerAngles = new Vector3(0, Random.Range(0, 360), 0);
float rand;
rand = Random.Range(0, 1);
if (rand >= 0.5f)
{
Instantiate(TwoSB, SpawnLocation, SpawnRotation);
}
else
{
Instantiate(TwoSBWR, SpawnLocation, SpawnRotation);
}
b -= 1;
}
c = OneSBTS;
while (c > 0)
{
Vector3 SpawnLocation;
SpawnLocation = new Vector3(Random.Range(-90, 90), 2.5f, Random.Range(-90, 90));
while (SpawnLocation.x < 15 && SpawnLocation.x > -15 && SpawnLocation.z < 15 && SpawnLocation.z > -15)
{
SpawnLocation = new Vector3(Random.Range(-90, 90), 2.5f, Random.Range(-90, 90));
}
SpawnRotation.eulerAngles = new Vector3(0, Random.Range(0, 360), 0);
float rand;
rand = Random.Range(0, 1);
if (rand >= 0.5f)
{
Instantiate(OneSB, SpawnLocation, SpawnRotation);
}
else
{
Instantiate(OneSBWR, SpawnLocation, SpawnRotation);
}
c -= 1;
}
}
public void Spawn(GameObject ToSpawn)
{
print("Call Successfull");
Vector3 SpawnLocation;
SpawnLocation = new Vector3(Random.Range(-90, 90), 2.5f, Random.Range(-90, 90));
while (SpawnLocation.x < 15 && SpawnLocation.x > -15 && SpawnLocation.z < 15 && SpawnLocation.z > -15)
{
SpawnLocation = new Vector3(Random.Range(-90, 90), 2.5f, Random.Range(-90, 90));
}
Quaternion SpawnRot;
SpawnRot = new Quaternion(0, 0, 0, 0);
SpawnRot.eulerAngles = new Vector3(0, Random.Range(0, 360), 0);
Instantiate(ToSpawn, SpawnLocation, SpawnRot);
print("(Call) Spawn Complete");
}
}
Here is the script that checks for overlaps:
using UnityEngine;
using System.Collections;
public class CollisionCheck : MonoBehaviour {
void OnTriggerEnter(Collider col)
{
if (col.gameObject.name != "Player" && col.gameObject.name != "Ground" && col.gameObject.name != "Bullet(Clone)")
{
GameObject.Find("GameController").GetComponent<CreateBuildings>().Spawn(gameObject);
print("Called");
Destroy(gameObject);
}
}
void OnTriggerStay(Collider col)
{
if (col.gameObject.name != "Player" && col.gameObject.name != "Ground" && col.gameObject.name != "Bullet(Clone)")
{
GameObject.Find("GameController").GetComponent<CreateBuildings>().Spawn(gameObject);
print("Called");
Destroy(gameObject);
}
}
}
Here is a video showing what happens with different values for ThreeSBTS, TwoSBTS, and OneSBTS:
https://drive.google.com/open?id=0B_HzgsC9lP3nRURlaDUxQlJIOEk
As you can see in the video, the Destroy(gameObject); seems to do nothing, and the more buildings there are to spawn, the more likely that this causes problems. I would like to have 2, 4, and 6 as the values, but it rarely spawns correctly. Does anyone know how to fix this?