Jump to content

this vs gameObject in Unity

Learn what this and gameObject refer to inside a Unity component. A short example shows how Destroy targets either the component or the entire GameObject.

readGameDev Team

A common cause of confusion is the difference between this and gameObject in Unity code.

this is the current component, so for example if inside Player.cs then Destroy(this) would destroy the Player component, but not the game object in the scene of hierarchy.

gameObject is the current game object. So in the example of a Player game object, with a Player.cs component script added then Destroy(gameObject) would delete the entire game object from the scene and hierarchy, not just the Player component.

I hope this helps

Check us out on social media