Check if object exists at a certain position

hi i am incredibly new to Unity and dont have much experience with java script Im trying to write a code that checks if an object exists below another object. but i do not know a way of checking weather the space directly below is free or not.

function Update () {

var TerrainPrefab1:Transform;
if(||| insert some function i dont know that checks if an object exists at a certain spot||||. != true) {
var create = Instantiate(TerrainPrefab1,myPosition.position + Vector3(0,-1,0),Quaternion.identity );
}

you get the idea, please help

Try a raycast. raycast can return all kinds of info with RaycastHit.collider. If you don’t know much about them, there are a few examples in the doc. It’s better than sending a collider to do the job cause sometimes colliders and triggers are funny about running into things instead of things running into them.

Thank you very much
i used Physics.Raycast and it worked beutifully

function Update () {
if(Physics.Raycast(transform.position,Vector3(0,-1,0),1) != true) {
var create = Instantiate(TerrainPrefab1,transform.position + Vector3(0,-1,0),Quaternion.identity );
}

works like a charm

1 Like