- Home /
Get Object at a position
I want to know how a get the information of an object at a position. I was using Physics.CheckSphere to check if there is or there isn't an object at a point. But, if there is I want to know wich one is it ( the tag ). I as creating a trigger object to collide with it and see tag, but is giving me lot of bugs. Anyone knows a better way?
Answer by Mike 3 · Aug 04, 2010 at 05:59 PM
If you use Physics.OverlapSphere it'll give you an array containing the colliders inside the given sphere - you can then easily get a tag from the closest of those
http://unity3d.com/support/documentation/ScriptReference/Physics.OverlapSphere.html
Answer by rosdi · Jun 08, 2016 at 06:02 AM
Very old question, but recently I had to do the same thing and this is how I solved it. In my game, I generate gameobjects programmatically... therefore when instantiated the object I also name the object with its x,y,and z position.
For example, I am creating a wall, each wall will have its position appended in its name:
float x = ...;
float y = ...;
float z = ...;
string prefabName = "Wall";
//instantiate the object
var gObject = (GameObject)Instantiate(Resources.Load("Prefabs/" + prefabName));
var pos = new Vector3(x,y,z);
gObject.transform.position = pos;
//name this object and append its location for easy retrieval by location
gObject.name = prefabName + x + y + z;
Later down the road if I want to find this wall, I simply do
var aWall = GameObject.Find("Wall" + x + y + z);