Home > database >  How to distinguish object types in unity hierarchy?
How to distinguish object types in unity hierarchy?

Time:10-07

How can you get what type object is? I'm looking at Panel and Image and they have almost the same view. How would I distinguished that mainMenu is a Panel and blankWindow is an Image, if I didn't know what is what? Panel:

enter image description here

Image:

enter image description here

CodePudding user response:

if(gameObjectToCheck.GetComponent<Rigidbody>() != null)
{
      //do something
}

or more safe way:

 if (gameObject.TryGetComponent(typeof(Rigidbody), out Component component))
 {
     //do something
 }
  • Related