Search Issue Tracker
Duplicate
Votes
1
Found in
2018.4
2019.3.4f1
2020.2
Issue ID
1249927
Regression
No
GetComponentInParent is returning null when the GameObject is a Prefab
How to reproduce:
1. Open the attached project (case-1249927.zip)
2. Click on the 'test' GameObject and make sure the 'MyPrefab' field is set 'Foobar' Prefab
3. Enter Play Mode
Expected result: methods 'GetComponentInChildren', 'GetComponent', 'GetComponentInParent' return non null Components and the according messages get displayed in the Console window
Actual result: 'GetComponentInParent' returns null
Reproducible with: 2018.4.23f, 2019.3.16f1, 2020.1.0b11, 2020.2.0a12
Notes:
All mentioned methods are working fine with instantiated Prefabs or regular GameObjects.
-
tconkling
Nov 27, 2023 20:00
This is incorrectly marked as a duplicate of an unrelated issue! In fact, you must pass `includeInactive=true` to get a non-null result when calling this function on an uninstantiated prefab (which is easy to unwittingly do via the `OnValidate` function, for example).
-
sarynth
Jan 11, 2021 07:27
This is very misleading. I'm traversing the prefab, and I can do a GetComponent<X>() and it works, then GetComponentInParent<X>() and it fails. Confused me for quite a while. Changing to GetComponentInParent<X>(true); works. Seems like it should be consistent. I would suggest in the prefab, that it does not require a true call unless it is actually inactive.
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
- PlasticSCM NullReferenceException on entering the Play Mode when Unity Version Control tab is added to the layout
- Shortcut Helper Bar does not appear when enabling it and disabling it
- "Property (urp_ReflProbes_BoxMin) exceeds previous array size" warning spams console after switching platforms
- Have to change Input Mapping Path twice for change to take effect when Auto-Save is enabled
- No Duplicate option is available when right clicking States in the Animator Window even though it's available via Shortcut
This is a duplicate of issue #1180794