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
- "EndLayoutGroup: BeginLayoutGroup must be called first" error is thrown when changing Shader Precision Model from the Build Profiles window
- White artifacts/outlines are visible in the Garden Scene when viewing at meshes from a distance
- Shader warnings "Sprite-Unlit-Default" are thrown after building 2D Platrformer Microgame Template
- [Android] HLSL shader becomes corrupted when running on an Android device
- Missing spaces in "Welcome to VR Mutiplayer Template Project" Welcome Dialog window
This is a duplicate of issue #1180794