r/Unity3D • • 2h ago

Question Unity Physics engine not ready when scene loads?

I have a main menu and then a scene that is loaded when the game starts. Every actor in the scene has a Physics.RaycastAll that detects nearby colliders with a certain tag on Start

I'd never had an issue when the scene was standalone, however now that I am using scene load async (single, not additive) it seems that the physics engine is not awake in order to find the raycast hits, because I debugged all the way down to the actual cast call to ensure that was what was missing;

The actors exist, the external colliders being tagged exist with their correct tags, there is no mistiming from awake/on enable/start functions across the project

However, my raycast makes no hits. I can only assume this is because the physics engine isn't awake when the scene is loaded this way, but considering these functions are being called all the way to Start, it seems unless there is a more straightforward workaround I need to either do a weird collider shake during an awake function, or I need to somehow await async for the next physics update

Anyone know how best to handle this?

2 Upvotes

7 comments sorted by

5

u/Mission_Turnip4430 2h ago

Physics doesn't tick until the first FixedUpdate after scene load, so Start fires before any collision data exists. Wrap your raycast in a coroutine that yields `WaitForFixedUpdate` once and you'll be fine. You can also just move the cast to the first `FixedUpdate` if you set a bool to make sure it only runs once.

1

u/shawn_overlord 1h ago

(Solved my other issue immediately by finding out I was calling code accidentally)

2

u/Immediate-Resolve-79 2h ago

Before adding a coroutine, try calling Physics.SyncTransforms() right before the RaycastAll. With autoSyncTransforms off (the default), anything moved or spawned during the load isn't pushed to the physics scene until the next simulation step, so casts in Start can miss it.

2

u/shawn_overlord 1h ago

This turned out to be the most succinct way without needing the coroutine yea

2

u/jl2l Professional 2h ago

1

u/shawn_overlord 1h ago

Thanks, I was already looking at this to try and guess how I would await the physics update and figured it out

1

u/Human_Status_3565 1h ago

Classic `LoadSceneAsync` timing issue! Even when `Start()` runs, querying `Physics.RaycastAll` immediately on `Start()` during an async single-scene load can hit before the physics world / transform sync is ready for that frame's spatial queries—especially if `Physics.autoSyncTransforms` is disabled or transforms were initialized right before `Start()`.

Here are the cleanest fixes in C#:

  1. **Sync transforms and wait one FixedUpdate before `Physics.RaycastAll`:**

    Instead of running the initial `Physics.RaycastAll` synchronously inside `Start()`, start a coroutine from `Start()` that calls `Physics.SyncTransforms();` and `yield return new WaitForFixedUpdate();` before running your raycast.

  2. **Check `Physics.autoSyncTransforms` and `Physics.simulationMode`:**

    In your Unity Physics settings, verify that `Auto Sync Transforms` is enabled and `Simulation Mode` is set to `FixedUpdate` (not `Script`), so async scene activation doesn't leave stale collider bounds on frame one.

  3. **LayerMask instead of tag-only filtering:**

    Passing a `LayerMask` directly into `Physics.RaycastAll(origin, direction, maxDistance, layerMask)` is faster and avoids filtering misses after the cast.

I'm Manish from Mool Shankar Studio (Unity C# & Android developer). If you'd like to share the actor script or scene setup, I can help debug and test the exact fix cleanly!