UnityEssentials
Small but useful tools and features for Unity
GameObjectExtensions.cs
Go to the documentation of this file.
1 namespace UnityEngine
2 {
6  public static class GameObjectExtensions
7  {
13  public static T GetComponentRequired<T>(this GameObject self) where T : Component
14  {
15  T component = self.GetComponent<T>();
16 
17  if (component == null) Debug.LogError("Could not find " + typeof(T) + " on " + self.name);
18 
19  return component;
20  }
21 
22  public static void SetLayerRecursively(this GameObject self, int newLayer)
23  {
24  if (null == self)
25  {
26  return;
27  }
28 
29  self.layer = newLayer;
30 
31  foreach (Transform child in self.transform)
32  {
33  if (null == child)
34  {
35  continue;
36  }
37  SetLayerRecursively(child.gameObject, newLayer);
38  }
39  }
40  }
41 }
static void SetLayerRecursively(this GameObject self, int newLayer)
static T GetComponentRequired< T >(this GameObject self)
Get a component. Log an error if it is not found.