Hi everyone, I want to check if a Gameobject contains a component such as a rigidbody. How exactly do you do that? I have been searching for an if statement that does that but I haven’t found anything.
it’s really simple
if(gameObjectToCheck.GetComponent<Rigidbody>() != null)
{
//do something
}
this function also returns that component you so can access the component using this.
For those stumbling upon this thread, I would not recommend the Best Answer’s approach. Instead, use the following.
CustomComponent CC = this.GetComponent<CustomComponent>();
if (CC) {
//Component is valid AND stored for later use, preventing any further GetComponent calls
CC.value = 5;
}
if(!CC){
//Result is null, thus not attached. An 'else' following the previous block also works here.
}
To add on to the answer given by CozyBear, you can do the same thing with cleaner syntax by using a simple extension method.
public static class Extensions
{
public static bool TryGetComponent<T>(this GameObject obj, T result) where T : Component
{
return (result = obj.GetComponent<T>()) != null;
}
}
That way you can check for the object and get a handle on the reference all in one go, like so:
if (gameObj.TryGetComponent<Component>(out var comp))
{
// Then you can use the component within this scope.
}
The benefit of this is that if you needed to have a whole bunch of these conditional blocks in a row, you wouldn’t need the clutter of declarations or checks ahead of the conditionals. For example:
var comp1 = gameObj.TryGetComponent<Component1>();
var comp2 = gameObj.TryGetComponent<Component2>();
var comp3 = gameObj.TryGetComponent<Component3>();
if (comp1 != null)
{
// Do stuff
}
if (comp2 != null)
{
// Do stuff
}
if (comp3 != null)
{
// Do stuff
}
Versus
if (gameObj.TryGetComponent<Component1>(out var comp1))
{
// Do stuff
}
if (gameObj.TryGetComponent<Component2>(out var comp2))
{
// Do stuff
}
if (gameObj.TryGetComponent<Component3>(out var comp3))
{
// Do stuff
}
It may seem like a small enough difference, but it certainly can make things a lot nicer in certain situations.
This was my solution, which also goes beyond the scope of the question.
private Button buttonLocal;
public void Start()
{
buttonLocal = gameObject.GetComponent<Button>();
}
If anyone is looking for “contains a certain component then use it”, this is what you’re looking for: TryGetComponent()
// The first way
if (gameObject.TryGetComponent(typeof(HingeJoint), out Component component))
{
component.name = "My Hinge";
}
// The second (and easier in my opion) way
if (gameObject.TryGetComponent(out Rigidbody rb))
{
rb.useGravity = true;
}
This is the most bizarre thing I’ve ever seen. In Unity 2021.2.3f1, GetComponent<Rigidbody2D>()
does NOT return null
if the Rigidbody2D is attached to a parent object, but the returned Rigidbody2D is still invalid. To properly test if the GameObject has a Rigidbody2D, you have to use:
Rigidbody2D rb2d = go.GetComponent<Rigidbody2D>();
bool found = rb2d && rb2d.gameObject.name != "null";
if (found){
//do stuff, rb2d is valid
}
This is only for Rigidbody2D. For all other components, GetComponent<T>()
returns null if it’s not found, even if it’s attached to a parent GameObject. I’ve got no clue why this is, but it’s what I’ve observed. And there is the fix for Rigidbody2D specifically.