Destroy GameObject

#Whats the difference between Destroy(gameObject) and Destroy(this.transform.gameObject)?

From my understanding the second is just a long way to say the same thing. ‘this’ refers to the script that is currently running, ‘.transform’ refers to the transform component where the script is located and then ‘.gameObject’ refers to the object that the transform component is attached to. You shouldn’t need the transform property since you’re jumping to it to jump away, instead this.gameObject should be enough.

Taking it a step further the code infers ‘this’ if it doesn’t know which property you’re talking about so it can be dropped all together and instead only gameObject is needed so you’re now left with Destroy(gameObject).

It’s kind of like saying to somebody “Hey I want you to eat from the fruitBowl, purple, Spanish, Grenache, fruit, grapes” instead you could just say “Eat the grapes” there’s no need to talk about the other details.

The important bit to remember is that you can’t just use ‘this’ ie Destroy(this) as it refers to the instance of the script not the gameObject itself or with the previous example eating me instead of the fruit.

1 Like

Thank you for answer. :+1::+1:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms