searching for gameobjects with tag at position?

Hello People of the Unity Forums, i Come to you today with a question you see i was trying to find if there was already a building at the position i was instantiating at but im not quite sure how to do such a thing. I have my code but it is quite broken.

 if (GameObject.FindGameObjectsWithTag("Building").Contains(gameObject.transform.position == cursorposint)) {
                Instantiate(buildings[selectedbuilding], cursorposint + new Vector3(0, -1, 10), transform.rotation);
                print("build");
            }

your help on the matter would be great.

Hi,

If your scene contains more than one game object with the tag “Building” you have to loop through the array of found game objects, and test each of their individual positions. .Contains(gameObject.transform.position == cursorposint) will not do that test.

2 Likes

thanks :slight_smile:

You could also approach this from the opposite direction. You could sphere cast from the target position, then loop through all returned objects to check for the building tag. If you find one, you know your answer.