paul. H
paul. H

Reputation: 53

How can I find out if there is a Gameobject at position xy in Unity2D

I need to find out if there is a Gameobject at a specific location in Unity2D. Is there a way on how I can do it?

Upvotes: 1

Views: 902

Answers (1)

Whatever you're doing that needs this design requirement is flawed. But...

You have three options:

  1. Fix your design so that you don't need this (e.g. store a list/array/dictionary of any object you're interested in and use that instead). I.E. this question is raising an XY Problem red flag.
  2. Fire a ray into the scene and see if it hits anything. Be careful here, a ray that starts inside a collider will ignore that collider (alternatively, use OverlapSphere). Desired searched objects must also have a collider attached.
  3. Iterate over the entire transform hierarchy of the scene and check the position of each game object (warning: position is a floating point value, be careful when comparing floats!).

Upvotes: 2

Related Questions