Skip to content

Instantly share code, notes, and snippets.

@Ashwinning
Created February 7, 2016 23:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ashwinning/551ab77201e89a352bff to your computer and use it in GitHub Desktop.
Save Ashwinning/551ab77201e89a352bff to your computer and use it in GitHub Desktop.
FindChildrenWithTag : An extension method that finds all children of a Transform with a specified tag, and returns an array. transform.FindChildrenWithTag("SpecifiedTag")
	/// <summary>
	/// Extension method for UnityEngine.Transform.
	/// Finds the children with tag.
	/// Returns an array.
	/// </summary>
	/// <returns>The children with tag.</returns>
	/// <param name="tag">Tag.</param>
	public static Transform[] FindChildrenWithTag(this Transform trans, string tag)
	{
		Transform[] kids = trans.GetComponentsInChildren<Transform> ();
		Transform[] kidsWithTag = new Transform[] {};
		foreach (Transform kid in kids) 
		{
			if (kid.tag == tag) 
			{
				kidsWithTag [kidsWithTag.Length] = kid;
			}
		}
		return kidsWithTag;
	}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment