Skip to content

Instantly share code, notes, and snippets.

@kaiyumcg
Created October 11, 2022 03:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaiyumcg/0d053969ada9297bcb58375879ced0c0 to your computer and use it in GitHub Desktop.
Save kaiyumcg/0d053969ada9297bcb58375879ced0c0 to your computer and use it in GitHub Desktop.
Find component in disabled parent gameobjects
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//Put this file in any location inside Asset Folder of unity project
//except any special folder. https://docs.unity3d.com/Manual/SpecialFolders.html
public static class Ext
{
internal static T ExGetComponentInParent<T>(this GameObject g, bool includeInactive = false)
{
var ts = g.GetComponentsInParent<T>(includeInactive);
return ts == null || ts.Length == 0 ? default : ts[0];
}
internal static T ExGetComponentInParent<T>(this Transform t, bool includeInactive = false)
{
var ts = t.GetComponentsInParent<T>(includeInactive);
return ts == null || ts.Length == 0 ? default : ts[0];
}
internal static T ExGetComponentInParent<T>(this MonoBehaviour script, bool includeInactive = false)
{
var ts = script.GetComponentsInParent<T>(includeInactive);
return ts == null || ts.Length == 0 ? default : ts[0];
}
internal static T ExGetComponentInParent<T>(this Component component, bool includeInactive = false)
{
var ts = component.GetComponentsInParent<T>(includeInactive);
return ts == null || ts.Length == 0 ? default : ts[0];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment