[HELP] Question: Destroy(gameObject);

So this is probably a really simple question, but why when we destroy our our Music Player can we just use the command:

Destroy(gameObject);

However, when we want our initial instance of the Music Player to persist we need the class ‘GameObject.’ As in:

GameObject.DontDestroyOnLoad(gameObject);

I notice that the GameObject class also has a method called Destroy, so I’m a little confused.

Thanks,

~Thom

1 Like

…this one is gunna get messy… I will do what I can :wink:

Ok… so,

Within the UnityEngine library you have Object, this is a base class for all objects that Unity can reference.

GameObject inherits from Object, this is a base class for all entities within Unity scenes.

Destroy is a method within Object, it is available via GameObject because GameObject inherits from Object. Destroy will remove a gameobject, component or asset.

Hope this helps? :slight_smile:


See also:

1 Like

It makes perfect sense! …I think. Just a follow up then:

Destroy(gameObject);
Object.Destroy(gameObject);
GameObject.Destroy(gameObject);

Would all work?

1 Like

Should do, the top.one you have listed is just short hand for the bottom one anyway.

Give it a try!

:slight_smile:

1 Like

Privacy & Terms