Search Issue Tracker
By Design
By Design in 2023.1.X
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
- Texture2D hash changes inside of an AssetBundle when rebuilding a SpriteAtlas bundle with an empty AssetPostprocessor Script enabled
- Aniso Level still applies when Generate MipMap is disabled in Texture Import Settings
- Mipmap Limit Groups long names are not truncated when creating a new Mipmap Limit Group with a long name
- “ArgumentException: Invalid double parameter.” error is thrown when Infinity is typed into the Fixed Timestep field
- GameObject becomes gray when using HDRP and STP together on macOS
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;
}
}
Resolution Note (2023.1.X):
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;
}
}