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
- NullReferenceException is thrown when assigning an Object to a custom DebugUI.ObjectField in Rendering Debugger
- SystemInfo.get_graphicsDeviceName() is generating garbage GC Alloc when using Metal graphics API
- Crash on D3D12CommonShader::ApplyGpuProgram when attaching material which samples "_UnityFBInput0" to "Full Screen Pass Renderer Feature" Component
- SpeedTree meshes and objects count differs when comparing the numbers in the Player with the Editor
- Model and Prefab Preview icons are not updated after upgrading associated Materials to URP
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;
}
}