Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Help with Physics2D.OverlapBox (C#)

I have a script that populates a 2D grid with objects, I am trying to add a function that will check if there is already an object on the grid space using Physics2D.OverlapBox but no matter what I do, the check returns true. I have double checked that the objects that I am checking for are all in layer 8 have have a 2D box collider on them.

 {  int layerMask = 1 << 8; //Layer 8
 
     // Rows and cols for the map grid
     public Vector2 myGrid;
 
     // Prefabs dimensions in Unity units
     public Vector3 tileDimensions;
 
     // Populate with all the prefabs used to generate map
     public GameObject[] prefabTiles;

     void Start()
     { CreateMap();}
 
     void CreateMap()
     {{ for (int row = 1; row <= myGrid.y; row++)
       { for (int col = 1; col <= myGrid.x; col++){ {
 
                         if (Physics2D.OverlapBox((new Vector2((col - 1) * tileDimensions.x, (row - 1) * tileDimensions.y)),(new Vector2(1f,1f)), layerMask) == false)
                         {  Debug.Log("FreeSpace");
                             int n = Random.Range(0, prefabTiles.Length);
                             GameObject theTile = Instantiate(prefabTiles[n], transform);
                             theTile.transform.localPosition = new Vector3((col - 1) * tileDimensions.x, (row - 1) * tileDimensions.y, 1);                    
                         }
                     }
                 }
             }
         }
     }
 }


Sign In or Register to comment.