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

Scene Management

-

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.

Comments (2)

  1. 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).

  2. 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

Log in to post comment

All about bugs

View bugs we have successfully reproduced, and vote for the bugs you want to see fixed most urgently.