Skip to content

Commit 6b94dfe

Browse files
Implemented GetComponentOrThrow
1 parent 3e7ea62 commit 6b94dfe

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

Runtime/EntityComponent.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,27 @@ public void SetActive (bool value) =>
4848
/// Destroys the game object of the entity.
4949
public void Destroy () =>
5050
UnityEngine.Object.Destroy (this.gameObject);
51-
51+
5252
/// Gets a component on an enity and sets it's reference to a property.
5353
public void GetComponentToProperty<UnityComponentType> (ref UnityComponentType entityProperty, bool includeChildren = false, bool includeInactive = false) =>
5454
entityProperty = includeChildren == true
5555
? this.GetComponentInChildren<UnityComponentType> (includeInactive)
5656
: this.GetComponent<UnityComponentType> ();
5757

58+
/// <summary>
59+
/// Tries to get a component on the entity and throws an exception if it's
60+
/// not found.
61+
/// </summary>
62+
/// <typeparam name="UnityComponentType">The Component type.</typeparam>
63+
/// <returns>A reference to the Component.</returns>
64+
/// <exception cref="System.Exception"></exception>
65+
private UnityComponentType GetComponentOrThrow<UnityComponentType> () {
66+
var _component = this.GetComponent<UnityComponentType> ();
67+
if (_component != null)
68+
return _component;
69+
throw new System.Exception ($"Unable to get Component of type {typeof (UnityComponentType)} on entity");
70+
}
71+
5872
/// Adds an asset to the entity.
5973
public UnityEngine.Object AddAsset (UnityEngine.Object asset) =>
6074
UnityEngine.Object.Instantiate (asset, UnityEngine.Vector3.zero, UnityEngine.Quaternion.identity, this.transform);

0 commit comments

Comments
 (0)