(SOLVED) instance = this;

Hi i was wondering if someone could go deeper into the meaning of (instance = this;)
I am not quite sure what its purpose is or what it even means.

Hello @MAPLE_MOOSE,

From memory this was to do with the Music Player game object wasn’t it?

Basically, this is the script which is attached to a game object. instance is the variable.

In this scenario you only want to have one instance of the Music Player, a singleton pattern is used. The code checks to see if an instance of the Music Player already exists, if it does it destroys the one that has just been created. If it doesn’t already exist, it sets instance (the single instance of the Music Player) equal to this.

Does this help?

2 Likes

Thanks @Rob for your response.

I am a bit confused however in the first half of the if statement we are Destroying (gameObject) while in the else we are assiging instance to “this” . Here I would like to know why is “this” keyword used instead of gameObject.

If I understand your last reply correctly it seems

gameObject -> refers to Music Player Object
this -> refers to MusicPlayer.cs script ?

We are destroying the gameObject in the first part of the if statement as this game object will have just been created when the scene loaded, the script being attached to it.

In the else part of the statement we are populating the static variable with this, which refers to the instantiated object, by which I refer to the music player (from its class etc).

In jargon free terms…

First time the game runs, no instance will exists so we can keep the game object that was created upon the scene loading and set the instance variable for our singleton pattern.

The next scene that loads creates the game object with the script attached again, we check again but because we already have the one and only one instance we want, we destroy the game object, which destroy the script component attached to it also, leaving the one instance of the game object and script which we need.

1 Like

Oh ok i see now thank you

1 Like

You are very welcome :slight_smile:

Followup questions:

is there anything special about the name ‘instance’? Could it be called SAM and work just as well or is instance specific for an instance of the class?

Trying to get my head around the special variable ‘this’. Is ‘this’ a pointer to the class we are in?

The static MusicPlayer named instance is declared on line 5 of https://github.com/CompleteUnityDeveloper/05-Block-Breaker/blob/master/Assets/Scripts/MusicPlayer.cs

You could declare it as instance, the_instance, PrincessBeatriceOfYork or almost whatever you like (excluding reserved words).

this is a different thing - it refers to the actual instance (in C#/OOP terms) of the class that has been instantiated and has hit that part of the script.

Say you and I are both instances of Person. When my brain refers to this, it means me. When your brain calls this, it means you. The “code” and “class” are the same, but we are different instances.

5 Likes

Privacy & Terms