Why static instance doesn't reset to null?

Hi guys,

I understood well how that works, but I still wonder why the static MusicPlayer instance = null; statement right at the beginning of the script does not reset the value of the static object to point nowhere (=null) once the new MusicPlayer Object is created once the Start Scene is reached again. I know what happens it that the script reaches the point of self-destruction and erradicates that object, but why is instance not driven again to null before that happens (static MusicPlayer instance = null) is really the thing that I still don’t see clear at all. Thanks in advance.

:slight_smile:

2 Likes

Hi Jousen!

Here’s whats happening:

The first time the Start Scene starts, it creates the instance of the Music Player, and thats the ONLY one that will be created. Next time you reach the start scene, another Music Player wants to be instantiated (let’s say that this second instance starts with null, and then it goes to the start method), but it realize thats there is a previous instance created, and it says: well, bye bye…

Hope i was clear with the explanation.

:slightly_smiling_face:

Hi Eschafir, thanks for your answer!

So if I got everything clear, then the reason is that the static variable of class MusicPlayer “instance” is ONLY and ONLY created and initialized the first time an instance of the MusicPlayer is generated, and once it exists, then that line of code “static MusicPlayer instance =null;” is just ignored due to the fact that the class already possesses this static variable, which is now pointing to the first instance created. Then the second and posterior coming new instances are destroyed. Is it like that? or I’m not interpreting it good?

I mean, the initialization of the “static MusicPlayer Instance=null;” is really being skipped because it already exists in the global class? This is really the critical point which I’m still not sure. Thanks.

:slight_smile:

Well I think I just talk too much to say too few. My real question is:

Why each time a new instance of MusicPlayer is created, the “static MusicPlayer instance” (the static variable of the MusicPlayer class) is not initialized to “null” and only does it the first time? Thanks in advance, buddies. :slight_smile:

Hmm I’ll take a crack at this. This is how I understand it. “static MusicPlayer instance = null” by itself doesn’t do anything. It’s only been initialized. It isn’t called until it’s put in to “void Awake ()” or “void Start ()” or others like them. Things aren’t called outside of these methods. It’s only in these methods where all the things happen! :stuck_out_tongue:

Would be great if someone could confirm this or expand on it. Hope it makes things clearer anyway!

Hi James, thanks for your answer.

I asked some friend of mine that works in software developement and he told me that what is happening here is that static variables are ONLY initialized one time as they are created. Afterwards, if the variable already exists (which happens here when the second MusicPlayer is created) then its value is not set again to “null”, since the static value already exists on the class, and so is the initialization command line of the static variable (static Musicplayer instance = null) ignored, once again, as it already exists, that is the key point here.

I hope this topic helped you guys :slight_smile:

Hi Guys!

I found this articles that may help to understand the concept of “static”. Hope you find it useful.

https://www.codeproject.com/Articles/15269/Static-Keyword-Demystified

https://r.je/static-methods-bad-practice.html

Thanks for asking this question and thanks to those who answered! I actually had the exact same issue with understanding what was happening and it helped clarify things a bit for me.

We create instance to hold a pointer to an instance of MusicPlayer. & initialize it to null. Then when the first class instance is created, we set instance to point to the new class instance that was just created using this. Future instances of MusicPlayer will see that instance is not null and will destroy themselves.

Hope its clear :slightly_smiling_face:

Thanks to everyone, I think it is already clear, I would summarize it as “A static is only and only initialized one time when it is instantiated”. Have fun!

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

Privacy & Terms