Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Physics2D.OverlapAreaAll only works when using editor

Discussion in 'Physics' started by TuxoAhjhj, Jul 3, 2023.

  1. TuxoAhjhj

    TuxoAhjhj

    Joined:
    Jan 12, 2023
    Posts:
    8
    I have a dungeon random generator that take predesigned rooms from a pool and connect thems. The rooms are prefabs with their own tilemaps, tilemap collider and environmental objects as their child gameObject.
    I have a script for the room prefabs with a function (lets call it function A) that check through each tile in the room, run a OverlapAreaAll to see if there’s any collider there, and if there arent, the position is saved in a list as a spawnable point for enemies.

    Here lies the problem. I’ve setup a Editor button so that I can manually run function A for each room to test if the function works, and it does. But when I loop through the all the rooms from the roomsManager gameObject and run function A for each, OverlapAreaAll can’t seem to detect any collider.

    Does anyone has an idea on what might be going on? Any idea would help as I can’t seem to find anyone with the same problem
     
  2. TuxoAhjhj

    TuxoAhjhj

    Joined:
    Jan 12, 2023
    Posts:
    8
    Update:
    It would seems the problem isn't with OverlapAreaAll, but with when it's ran after Instantiating the room prefabs. When I add an IEnumerator to wait for 1 second before running OverlapAreaAll, it can detect the colliders in the child objects of the room just fine. I'm not sure if that's how it's supposed to be though or there's something wrong
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    There's no time dependency on any physics query. As soon as you add it, it's there.

    A common mistake is to instantiate a prefab without specifying its position/rotation in the instantiate call and then setting the Transform immediately afterwards. Transform changes don't get read until the simulation runs which, by default, is at 50Hz so not 1Hz (1 sec).

    Either way, it'll be something you're doing.

    Try creating a collider then immediately querying it. Without fail, it'll be detected 100% of the time.
     
    TuxoAhjhj and Unifikation like this.
  4. TuxoAhjhj

    TuxoAhjhj

    Joined:
    Jan 12, 2023
    Posts:
    8
    I see, thanks a lot for your input. I’ll check it and comeback to you if there’s anything