How to: Dont spawn on other objects ?
Hi,
i want some prefabs to spawn at a point where no collider-object is located.
my spawn function:
public void SpawnSpeedup()
{
int x = (int)Random.Range(border_l.position.x, border_r.position.x);
int y = (int)Random.Range(border_t.position.y, border_b.position.y);
Instantiate(speedup_prefab, new Vector2(x, y), Quaternion.identity);
}
How can i do this ?
My question is similar to this: http://answers.unity3d.com/questions/849571/collision-spawned-object.html
Please , someone? To explain a bit more: -the objects should spawn at an empty place on the gamefield -i know i could do this with array but i think there is an easier way if i say: you spawned on a collider object ? then destroy it and spawn new , but the problem is that the collider functions at these objects are already used for other stuff.
Answer by Baste · Oct 27, 2015 at 02:50 PM
Two main ways:
1: Use raycasts to check if the spot is already occupied.
2: Add all possible spawn points to a list, select a random element from that list as a spawn point, and remove each selected spawn point from the list.
Use the first one if you also want to avoid pre-placed objects, use the second one otherwise (as it's probably easier to get right).
Your answer
![](https://koobas.hobune.stream/wayback/20220612042836im_/https://answers.unity.com/themes/thub/images/avi.jpg)