Question by
mrbubifish_unity · Feb 08, 2020 at 12:24 PM ·
instantiateprefabcrashspawn
Crash when instantiating two Prefabs with the same tag
Somehow Unity crashes when I instantiate the SpawnVector Prefab twice, I've also tried manually creating GameObjects in script and setting their tags to Spawnpoint, but it still crashes whenever the two spawnpoints have the same tag. The SpawnVector Prefab is nothing but an empty GameObject with the "SpawnPoint" tag.
//Sets spawnpoints for goal and player to be the maximum distance apart from eachother
void SetSpawnPoints(List<GameObject> objects)
{
float[,] distances = new float[objects.Count, objects.Count];
for (int i=0; i < objects.Count; i++)
{
for (int j=0; j < objects.Count; j++)
{
distances[i, j] = Vector3.Distance(objects[i].transform.position, objects[j].transform.position);
}
}
float max = distances.Cast<float>().Max();
bool BreakFlag = false;
for (int i = 0; i < objects.Count; i++)
{
if (BreakFlag)
{
break;
}
for (int j = 0; j < objects.Count; j++)
{
if (distances[i, j] == max)
{
GameObject Spawn1 = Instantiate(SpawnVector, objects[i].transform.position, Quaternion.identity);
GameObject Spawn2 = Instantiate(SpawnVector, objects[j].transform.position, Quaternion.identity); //crashes for some reason
Spawn1.transform.SetParent(GameObject.Find("SpawnPoints").transform);
Spawn2.transform.SetParent(GameObject.Find("SpawnPoints").transform);
BreakFlag = true;
break;
}
}
}
}
Comment