- Home /
Don't allow prefab to spawn if prefab already exists at the spawn location.
I want to make it so that if there is already a prefab spawned at the specified positions, then it won't spawn another on top of it.
This code, however, doesn't seem to do that when I run the game. Any idea where I went wrong?
(This is only a part of my spawner script, but this is the part I am trying to focus on, which is why I only included it.)
void spawnAtB()
{
if (prefabToSpawn.transform.position.x == 65.61 && prefabToSpawn.transform.position.y == 14.22905 && prefabToSpawn.transform.position.z == 66.2)
{
return;
}
else
{
Instantiate(prefabToSpawn, peekerSpawnB.transform.position, peekerSpawnB.transform.rotation);
}
}
void spawnAtC()
{
if (prefabToSpawn.transform.position.x == 66.10021 && prefabToSpawn.transform.position.y == 14.22905 && prefabToSpawn.transform.position.z == 0)
{
return;
}
else
{
Instantiate(prefabToSpawn, peekerSpawnC.transform.position, peekerSpawnC.transform.rotation);
}
}
void spawnAtD()
{
if (prefabToSpawn.transform.position.x == 66.01 && prefabToSpawn.transform.position.y == 14.22905 && prefabToSpawn.transform.position.z == -64.52)
{
return;
}
else
{
Instantiate(prefabToSpawn, peekerSpawnD.transform.position, peekerSpawnD.transform.rotation);
}
}
void spawnAtE()
{
if (prefabToSpawn.transform.position.x == 0.33 && prefabToSpawn.transform.position.y == 14.22905 && prefabToSpawn.transform.position.z == -67.54)
{
return;
}
else
{
Instantiate(prefabToSpawn, peekerSpawnE.transform.position, peekerSpawnE.transform.rotation);
}
}
void spawnAtF()
{
if (prefabToSpawn.transform.position.x == -64.51 && prefabToSpawn.transform.position.y == 14.22905 && prefabToSpawn.transform.position.z == -66.25001)
{
return;
}
else
{
Instantiate(prefabToSpawn, peekerSpawnF.transform.position, peekerSpawnF.transform.rotation);
}
}
void spawnAtG()
{
if (prefabToSpawn.transform.position.x == -66.10021 && prefabToSpawn.transform.position.y == 14.22905 && prefabToSpawn.transform.position.z == 0)
{
return;
}
else
{
Instantiate(prefabToSpawn, peekerSpawnG.transform.position, peekerSpawnG.transform.rotation);
}
}
void spawnAtH()
{
if (prefabToSpawn.transform.position.x == -66.59 && prefabToSpawn.transform.position.y == 14.22905 && prefabToSpawn.transform.position.z == 65)
{
return;
}
else
{
Instantiate(prefabToSpawn, peekerSpawnH.transform.position, peekerSpawnH.transform.rotation);
}
}
}
Answer by bakir-omarov · Jun 10, 2018 at 10:10 PM
You can use Physics.OverlapSphere for it. It will check if there is something at the spawn point, if not, then you can spawn, but if there is some collider, it means you already spawned here, so just wait. Also if your your spawn points on ground (y = 0). just uncomment line in code, and it will raise overlap position to 1.5f above the spawn point.
void spawnAtB()
{
float _overlapRadius = 1f;
Vector3 _overlapPosition = peekerSpawnB.transform.position; // paste here your spawn point position
//_overlapPosition = new Vector3(_overlapPosition.x, _overlapPosition.y + 1.5f, _overlapPosition.z); //uncomment it if your spawn point y=0 (grounded)
Collider[] hitColliders = Physics.OverlapSphere(_overlapPosition, _overlapRadius);
if (hitColliders.Length != 0)
{
Debug.Log("There is something in this spawn spot. I can't spawn here anything.");
}
else
{
Instantiate(prefabToSpawn, peekerSpawnB.transform.position, peekerSpawnB.transform.rotation);
}
}
Your answer
Follow this Question
Related Questions
Why is Prefab declared as type Transform? 2 Answers
instatiates at a bad position 1 Answer
Instantiating an object as a child of another object 1 Answer
Instantiating an array of objects - how can I instantiate a certain prefab only once? 1 Answer
Having multiple objects fire prefabs in different times C# 0 Answers