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
- Physics.Raycast fails when a Capsule Collider at a default position is used
- [Silicon] Crash on mono_get_hazardous_pointer when disconnecting Bluetooth Headhpones while in Play Mode
- Switching Project when importing complete project does not import all assets and project opens incomplete
- [Linux] Input.mouseScrollDelta is inconsistent when limiting framerate
- Undoing the branch rotation resets only wireframe and not mesh when undoing the action first time after launching the project
This is a duplicate of issue #1180794