[Solved] How does the second Music Player in another scene destroy itself?

  1. So he created another Music Player in the Game Scene, and then ran it starting in the Game Scene. From that point, this is the first time the script has been run, so wouldn’t this instance be the first and take the role of the Music Player, and then when we get to the Start Menu, wouldn’t that Music Player be the second one and be destroyed?

  2. I also want to ask about “this.” Is it referring to the current instance reading the script? So the first instance is created and that instance is “this instance” and placed into “instance?” Why isn’t “this” used in Destroy? Can’t “this” be used in place of “gameObject?”

  3. “Instance” is a variable name right? I actually thought it might be a special keyword until I realized it could just be a name. Does the variable name have to be “instance” in this case or can it be anything else?

Hi @gameangel147,

  1. Can you provide me with the from time in the lecture so I can check this in context please.

  2. this refers to the script itself.

    In most cases, you don’t want to simply destroy a script, which is a component of a GameObject, you actually want to destroy the GameObject itself, with everything that is a component of it. By using Destroy(gameObject) you destroy the GameObject which the script is a component of, and everything else it has a component (the script included).

  3. instance is just a variable name in this scenario, as you have correctly identified and yes, it could be called anything you want, however, as with all good practices, it makes sense that the name represents what it is… for example, MusicPlayer.instance certainly gives me the impression that I am dealing with an instance of the MusicPlayer.

    You could of course just call it Fred, but MusicPlayer.Fred seems a little meaningless :slight_smile:

1 Like

Ok, your explanations helped. The thing is that normally I can’t tell when they’re using a name or a keyword, and here I thought it was a keyword.

As for the first question, you can start at 5:58. He adds a duplicate music player into Level 1 and plays it from there, and the console logs “Duplicate music player self-destructing,” and I"m confused as to what happened there. Wouldn’t he have to go to Game Over, then the main menu and back to level 1 for a duplicate to be created? Any explanation there would be appreciated.

Also wanted to ask about “null.” Is that just a value, kind of like “true” and “false,” but here it’s another way of saying zero? What are the best times to use null?

1 Like

Hi @gameangel147,

To answer your queries in order;

  • typically in C# you will find that the data type is stated before the variable name, for example;

    string message = "Hello GameAngel147";
    

    in the above, the type is specified first string, it is then followed by the name of the variable message, after this, we have also populated the variable with a value bewteen the two " characters.

    The same is true of classes you create yourself, for example;

    MusicManager funkyMusicManager;
    

    in the above, our type is specified first MusicManager, followed by the name of the variable funkyMusicManager. In this example we are only declaring the variable not populating it, or initialising it.

    In this examples, the variable name can be anything you like, but it is good practice to name your variables (and properties, methods and classes) to something which they represent. So, my first example is perhaps better than my second as the term “funky” doesn’t really mean anything to us, unless we wanted to be really specific and only play “funky” music I suppose :slight_smile:

    Within the IDE, whether it is MonoDevelop or Visual Studio, you should be able to hover your mouse over either the variable, which will tell you which data type it is, or, over the type itself and you will note there are differences in the text that is displayed.

  • Regarding the lecture (05:58), ok, so what Ben is explaining here is that, in the game we are only adding one MusicManager game object, because only one is needed. It has been set to not be destroyed and only one instance of it will ever exist. Ben mentions that “but if we did add one, this code would protect us”, what Ben is stating is that, although there isn’t any reason to add another MusicManager to any of the other scenes, if someone accidentally did, the following code;

    if (instance != null)
    {
        Destroy(gameObject);
        Debug.Log("Duplicate MusicPlayer self-destructing!");
     }
     else
     {
         instance = this;
         GameObject.DontDestroyOnLoad(gameObject);
     }
    

    Within the MusicManager you have a static variable called instance, again it could be called anything, but this represents an instance of the initialised MusicManager. It has been defined as static which means that the variable effectively belongs to the class, not the instance of the class.

    So, when you create a MusicManager instance for the first time, as one doesn’t exist already, the code hops over the first part of the if statement, as instance != null isn’t true, because it does equal null (I’ll cover null below).

    The code runs into the else code block instead which then sets the instance variable to equal this, this being the instance of the MusicManager script that is executing. The code then states that the GameObject belonging to this script is not to be destroyed when the OnLoad method is called, so for example, when the next scene loads, and all of the objects in the previous scene are destroyed, this one shouldn’t be.

    At this point you now have a variable called instance which has been set to the instance of the MusicManager script, and because it is static it belongs to the class, so it will be persisted, even if the instance of the GameObject isn’t.

    This is why you do not need to add a MusicManager GameObject to any other scenes, because we are able to reference the instance variable from the MusicManager class and access the one we have already created.

    Now, if another MusicManager GameObject was added to another scene, this code would be called for a second time, however, this time through, the if part of the if statement would be true instance != null, instance is no longer null, it is now equal to the instance we created of the MusicManager. So, the instance that we have just created again in the new scene, we don’t want or need, so we call the Destroy method and pass in the reference to the scripts GameObject. This then destroys the second instance of the MusicManager, leaving only the first one we created in place.

    And yes, you are correct, assuming no other MusicManager GameObject was added to any other scenes, then you would have to return to the first scene where it was added for that piece of code to be triggered, however, in the lecture, Ben demonstrates this by adding another MusicManager GameObject.

  • Regarding null, think of null as nothing, it isn’t anything at all. Don’t confuse it with zero, as zero is an integer value. Value types will have default values, in the case of an int its zero, bool its false. null is the default value for reference types.

    You will often see null being used to clear the reference of a reference type, or in a test, where you may want to perform an action if the variable/object is null.

I hope the above is of use. :slight_smile:


See also;

1 Like

Thanks for clearing all of that up, though as for the music player, that wasn’t exactly what I was asking.

I know what the script is doing and how it works, but my confusion comes from the fact that he started in the Level Scene and the Music Player in that scene was automatically destroyed. I figured that since we started in that scene, that Player becomes the reference for Instance, and if we then go to the Main Menu scene, the player in there is now the second one and that one gets destroyed.

I figure that since we start in the Level scene, we haven’t been to the Main Menu, so the Music Player in the Main Menu hasn’t been created, but the Level Music Player gets destroyed anyways. I’m thinking that because Level is the second scene in the Build Settings, perhaps Unity creates the Main Menu scene as well as the first Music Player then creates the Level scene and that’s how the Level Music Player becomes the second one and ends up destroyed.

In short, why does the Level Music Player get destroyed when we start in the Level Scene?

As for null, I get it, and thanks for the links. Also sorry for making you go to so much trouble, and I appreciate all the work you put into helping me and everyone else. :slight_smile:

Hi,

Sorry for the delay responding.

but my confusion comes from the fact that he started in the Level Scene and the Music Player in that scene was automatically destroyed.

If you watch closely, although there is a little bit of flicking back and forth between the scenes, the game is actually started on the Start scene (06:21) - the second Music Player was added to the Level scene before the game was run.

Hope this helps :slight_smile:

1 Like

Okay, I see. I’m not sure how I missed that. I thought they started in the Level_1 Scene.

Nevermind then, thanks! :slight_smile:

1 Like

No worries, it is easy to do but glad it resolves the query for you :slight_smile:

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

Privacy & Terms