Search Issue Tracker
By Design
Votes
1
Found in
2022.1.23f1
2022.2.0b15
2023.1.0a19
Issue ID
UUM-19216
Regression
Yes
FindObjectsOfType doesn't find objects when invoked in a method with RuntimeInitializeOnLoadMethod Attribute and loadType of BeforeSceneLoad
See forum thread here: https://forum.unity.com/threads/findobjectsoftype-in-beforesceneload-no-longer-finds-anything-in-2022-2.1358558/
How to reproduce:
1. Open the user’s attached “WhenThingsHappenTest” project
2. Enter the Play Mode in the “SampleScene” Scene
3. Observe the Console log
Expected result: “numItems = 1” is logged
Actual result: “numItems = 0” is logged
Reproducible with: 2022.1.0a12, 2022.1.23f1, 2022.2.0b15, 2023.1.0a19
Not reproducible with: 2020.3.42f1, 2021.3.14f1, 2022.1.0a11
Reproduced on: macOS 13.0 (Intel)
Comments (1)
-
Snowdrama
Nov 16, 2022 17:42
I also had this issue that I documented over in a forum thread here:
Add comment
All about bugs
View bugs we have successfully reproduced, and vote for the bugs you want to see fixed most urgently.
Latest issues
- Memory leak when a lot of UI elements are spawned and despawned
- Warnings are not logged in the Console window when using external code analyzers
- Errors “TLS Allocator ALLOC_TEMP_TLS, underlying allocator ALLOC_TEMP_MAIN has unfreed allocations, size 288“ appear constantly when Prefab is open
- Crash on PPtr<Shader>::operator or NullException errors spammed in console when calling Dispose() on null GraphicsBuffer with baked Reflection Probes
- “EndRenderPass: Not inside a Renderpass” and other Render Graph errors in the Player when Render Graph is enabled and Overlay UI is used
Resolution Note:
The RuntimeInitializeLoadType.BeforeSceneLoad callbacks are invoked very early in the load scene operation of the first scene: just after the objects are loaded in memory but before Awake and OnEnable is called. So basically you get this callback before the objects loaded have been fully setup. The change in behavior for finding active objects is here to stay since objects are not actually 'activated' before Awake and OnEnable are called. This change was need to fix various systems.
What you need to do now when using callbacks that are invoked before Awake() is to get all inactive objects and use the state for 'activeSelf' and manually check if the object will be active after Awake() by checking all parents are also activeSelf. In other words activeInHierarchy will always return false until the GameObjects have been awakened.
The docs for RuntimeInitializeLoadType are incorrect and will be corrected to match when these callbacks are invoked and more info about the lifetime object state.
Example:
class MyClass
{
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
private static void FirstSceneLoading()
{
var components = UnityEngine.Object.FindObjectsByType(typeof(MonoBehaviour), FindObjectsInactive.Include, FindObjectsSortMode.None);
var willBeActiveAfterSceneLoad = 0;
foreach (var c in components)
{
if (WillBeActiveAfterSceneLoad(((Component)c).gameObject))
willBeActiveAfterSceneLoad++;
}
Debug.Log("BeforeSceneLoad. Will be Active after Awake, count: " + willBeActiveAfterSceneLoad);
}
static bool WillBeActiveAfterSceneLoad(GameObject gameObject)
{
Transform current = gameObject.transform;
while (current != null)
{
if (!current.gameObject.activeSelf)
return false;
current = current.parent;
}
return true;
}
}