Find with Component#
Game Object / Find with Component
Can Read
Cannot Write
Description#
Finds and returns the first Game Object that has the specified Component attached
Parameters#
| Name | Description |
|---|---|
| Type | The Component type searched for |
| Include Inactive | Whether Game Objects that are not active are also searched |
Code#
using System;
using UnityEngine;
using VisualScript.Icons;
using Object = UnityEngine.Object;
namespace VisualScript.Runtime
{
[Get]
[Title("Find with Component")]
[Category("Game Object/Find with Component")]
[Description("Finds and returns the first Game Object that has the specified Component attached")]
[Image(typeof(IconCubeSolid), ColorTheme.Type.TextLight, typeof(BadgeMagnifier))]
[Parameter("Type", "The Component type searched for")]
[Parameter("Include Inactive", "Whether Game Objects that are not active are also searched")]
[Keywords("Search", "Reference", "Object", "Scene", "Script", "Behaviour")]
[Serializable]
public class TypeGameObjectFindWithComponent : TypeGameObject
{
[SerializeField] private TypeReferenceComponent m_Type = new TypeReferenceComponent();
[SerializeField] private GetBool m_IncludeInactive = new GetBool(false);
public override GameObject Get(Args args)
{
Type type = this.m_Type.Type;
if (type == null) return null;
FindObjectsInactive includeInactive = this.m_IncludeInactive.Get(args)
? FindObjectsInactive.Include
: FindObjectsInactive.Exclude;
return Object.FindAnyObjectByType(type, includeInactive) is Component component
? component.gameObject
: null;
}
public override string ToString()
{
return $"Find {this.m_Type}";
}
protected override SubscriptionMode Subscription => SubscriptionMode.OnChange;
}
}