Checking for a gameobject at a position

I am trying to make a spawner button for my 2D-TBS game, i have gotten the object to spawn, but i am still trying to make a check to see if there is a game object at that location, other wise i get objects overlapping and pushing each other out of the way, i am drawing a complete blank on how to do it code wise, but i do know i need colliders, so i have place Boxcollider2Ds on everything thats gonna collide with something on the same layer

my code is included below, my objective here is to check if there is a gameobject at a current position, and if there is an object at that position, to spawn the object at a different position instead,

using UnityEngine; using System.Collections; using UnityEngine.UI; using Random = UnityEngine.Random;

1 Reply

I think that you can use Physics.Linecast from top to bottom, assigning the correct layer and check if there is any collision with a player or whatever are you instantiating. Maybe there are options more elegant, but this is what I should do in 5 minutes.
http://docs.unity3d.com/ScriptReference/Physics.Linecast.html