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
- Fixing Message Loss in UMPE During Domain Reload
- The Editor freezes when ScheduleReadOnly of IJobParallelForTransform with dependency is used
- Text is truncated in the Inspector when the display scale is set to 150%
- CancellationTokenSource(<timeout>) has incorrect cancellation status when used in WebGL Player
- VFX property value names are cut off when properties with 'Arc' are viewed in the VFX Blackboard
This is a duplicate of issue #1180794