I am writing a method to check if a gameObject has a component.
Here it is:
public static bool HasComponent <T>(this GameObject obj)
{
return obj.GetComponent<T>() != null;
}
And I'm using it like this:
void Update()
{
if (Input.GetKey("w"))
{
if (gameObject.HasComponent<Rigidbody>())
{
print("Has a rigid body.");
return;
}
print("Does not have rigid body.");
}
}
The gameObject does NOT have a rigid body but it is still printing that it does have.
It is just...
Note that you forgot the
where T:Component
part of the first line!
With that syntax error, the extension is meaningless: it's always finding "some component" since T is "blank".
NOTE.
Explanation of "what the heck is an Extension".
For anyone reading this who is not familiar with categories in c# .. that is to say "Extensions" in c# ... here's a easy tutorial ...
Extensions are critical in Unity.
You use them in pretty much every line of code.
Basically in Unity you do almost everything in an Extension.
Note that because extensions are so common, the OP did not even bother showing the wrapper class. Extensions always sit in a file like this:
In the example I included three typical extensions. Normally you'd have dozens or even hundreds of extensions in a project.
(You may prefer to group them in different
HandyExtensions
files, or just have one enormousHandyExtensions
file.)Every engineer and team has their own "common extensions" they use all the time.
Here's a typical example question about a subtlety of extensions in C#.
Note that in older languages you usually call extensinos a "category".
In c# it is an "extension", but people use the terms either "category" or "extension" all the time.
As I say, you use these constantly in Unity. Cheers
If you like that sort of thing, here's a beautiful one: