GetOrAddComponent
From Unify Community Wiki
(Difference between revisions)
Createdbyx (Talk | contribs) m (Bah! forgot the colon!) |
Isaiah Kelly (Talk | contribs) (Greatly improved the code) |
||
Line 1: | Line 1: | ||
− | + | This simple extension method will prevent you from having to write very redundant and ugly code every time you need to get a component but are not sure if it actually exist already. | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
<syntaxhighlight lang="csharp"> | <syntaxhighlight lang="csharp"> | ||
− | + | using UnityEngine; | |
+ | |||
+ | static public class UnityEngineExtensions | ||
+ | { | ||
+ | /// <summary> | ||
+ | /// Returns the component of Type type. If one doesn't already exist on the GameObject it will be added. | ||
+ | /// </summary> | ||
+ | /// <typeparam name="T">The type of Component to return.</typeparam> | ||
+ | /// <param name="gameObject">The GameObject this Component is attached to.</param> | ||
+ | /// <returns>Component</returns> | ||
+ | static public T GetOrAddComponent<T>(this GameObject gameObject) where T : Component | ||
+ | { | ||
+ | return gameObject.GetComponent<T>() ?? gameObject.AddComponent<T>(); | ||
+ | } | ||
+ | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | == | + | == Usage == |
+ | Create a new script named '''UnityEngineExtensions''' or whatever you like and put the above code in it. Then simply replace any '''GetComponent''' calls with '''GetOrAddComponent''' to ensure the component exist without having to check for null. | ||
<syntaxhighlight lang="csharp"> | <syntaxhighlight lang="csharp"> | ||
− | + | using UnityEngine; | |
− | + | ||
− | + | public class ExampleScript : MonoBehaviour | |
− | + | { | |
− | + | Rigidbody m_Body; | |
− | + | ||
− | + | void Awake() | |
− | + | { | |
− | + | m_Body = gameObject.GetOrAddComponent<Rigidbody>(); | |
− | + | } | |
− | + | ||
− | + | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | |||
+ | [[Category: C Sharp]] | ||
+ | [[Category: Extensions]] |
Latest revision as of 23:54, 3 February 2019
This simple extension method will prevent you from having to write very redundant and ugly code every time you need to get a component but are not sure if it actually exist already.
using UnityEngine; static public class UnityEngineExtensions { /// <summary> /// Returns the component of Type type. If one doesn't already exist on the GameObject it will be added. /// </summary> /// <typeparam name="T">The type of Component to return.</typeparam> /// <param name="gameObject">The GameObject this Component is attached to.</param> /// <returns>Component</returns> static public T GetOrAddComponent<T>(this GameObject gameObject) where T : Component { return gameObject.GetComponent<T>() ?? gameObject.AddComponent<T>(); } }
[edit] Usage
Create a new script named UnityEngineExtensions or whatever you like and put the above code in it. Then simply replace any GetComponent calls with GetOrAddComponent to ensure the component exist without having to check for null.
using UnityEngine; public class ExampleScript : MonoBehaviour { Rigidbody m_Body; void Awake() { m_Body = gameObject.GetOrAddComponent<Rigidbody>(); } }