Not destroying music player

Hi Andrew,

If you can’t see the second music player, how do you know it’s there?

One thing you may have done is added the MusicPlayer.cs script to another GameObject, so if you are only looking for a GameObject called Music Player for example, it could be that you’ve added the MusicPlayer.cs script component to the Main Camera or another object.

If you right-click on the MusicPlayer.cs you can choose “Find all references in scene” which will filter the Hierarchy and may help you find your duplicate.

Hope this helps :slight_smile:

I left the print instruction in that displays on the console how many music players are present
in the scene
after a couple seconds into the level it tells me 2 players are in the scene - never any more than that

find references in scene shows one in the splash screen and one in the main level
but the code should destroy one of them

Do you have the music player in both scenes? and are they both prefabbed? Before trying anything please save work via zip or your control choice. Try to remove scenes in the build and try then? Do both scenes have the music player attached with the.cs attached to both music players? If not maybe just delete the music player prefab in the scene it doesn’t destroy from hierarchy & or from the scene then replace from the prefabs folder put back in the scene prefab folder and the hierarchy. Hope this helps because your code is accurate compared to mine. :slight_smile:

Hi Andrew,

What does the output from the console look like?

Presumably, if you play the game from the Splash scene, it would say 1 initially, then as you move into the next scene you’d get a second row in the console that would say 2? Or, are you seeing it say 1 twice, on two rows?

Please note, it’s better to copy/paste your code and apply the code fencing characters, rather than using screenshots. Screenshots are ideal for displaying specific details from within a game engine editor or even error messages, but for code, they tend to be less readable, especially on mobile devices which can require extensive zooming and scrolling.

You also prevent those that may offer to help you the ability to copy/paste part of your code back to you with suggestions and/or corrections, meaning that they would need to type a potentially lengthy response. You will often find that people are more likely to respond to your questions if you make it as easy as possible for them to do so.

Hope this helps :slight_smile:


See also;

I’ll try reloading the scenes but yes the music player is prefabbed

As far as I can tell ive done it as the video states

It could be something in the settings in the inspector or like Rob stated you are not aware of musicplayer.cs attached to another game object. Do you save the game If yes then try the other steps I suggested if you do? If all fails before saving a copy- copy a working(older) saved a version before error accrued then save current game via zipping follow the other steps I suggested I only tell you this is because Unity is particular sometimes and doesn’t work properly only sometimes. keep me posted remember to save game the older saved version and then the current version in separate zip files. Keep me posted. :slight_smile:

Andrew,

Looking at your code again, you are outputting the count of objects that are of type MusicPlayer before you destroy them.

So, when you run the game the splash scene creates one and outputs the count of one, when you move to the second scene, the count is output before any GameObject is destroyed, thus the output would show as two. Then the GameObject would be destroyed.

Try adding this to your code;

private void Awake()
{
    int numMusicPlayers = FindObjectsOfType<MusicPlayer>().Length;

    print("Number of music players 'before' action is taken in this scene : " + numMusicPlayers);    // remove this line after testing

    if(numMusicPlayers > 1)
    {
        print("Destroying MusicPlayer");    // remove this line after testing
        Destroy(gameObject);
    }
    else
    {
        DontDestroyOnLoad(gameObject);
    }

    numMusicPlayers = FindObjectsOfType<MusicPlayer>().Length;    // remove this line after testing

    print("Number of MusicPlayers 'after' action is taken : " + numMusicPlayers);    // remove this line after testing
}

These changes can be removed after you’ve tested. Basically, you’ll get your original message with the slight tweak, then, after the if statement you’ll get the count again and output it. See what that total says in the message.

Hope this helps :slight_smile:

Thanks , I’ll give it a go when I get home from work later

1 Like

So this is the message I get in the console, I’m stumped
As far as I can tell, I’ve followed everything exactly

he has a prefabbed musicplayer with script on splash screen and level 1 with the same code
why is mine behaving differently?
musicplaterscriptoutcome

Hi Andrew,

Can you zip up your project files and share them with me please, probably easier at this stage for me to just look at what’s going on in the project and reply.

The forum will allow uploads of up to 10MB, if your project files (zipped) are larger than that you would need to use a service such as Google Drive or Dropbox, and then share the URL.

https://drive.google.com/open?id=104T9wksSMCwUynMbqKbWxFng7m0yQijh

not exported it before so hopefully link works (goes to google drive)

Hi Andrew,

Rather than creating a package, can you just zip up the project files, e.g. the whole working directory. This will give me the solution and project files also which will then not require me to create a new project in Unity and so on - tends to speed things up a bit.

If the project is fairly large, which I suspect it may be, please exclude the /library/ directory from the zip. Thanks.

Hi ok, will do after work then

Wasn’t sure on best way to send at the time

Yeah that’s ok. Often a unity package is great, but when its for the entire project its a little less helpful, at least for myself, due to the above - I try to be fairly lean with the time etc :slight_smile:

Actually fell asleep the second I got home last night XD
I’ve zipped up the project files, it’s only large because of the asset packs
pieces should be labelled fairly clearly
https://drive.google.com/open?id=1RyjqAgGLe8tbfUoOQ7Od0hMf_0BRj44M

Hi Andrew,

I’ve taken a look, it was a bit easier to see in context etc.

Running the game if I go from the Splash scene to Level 1, the Hierarchy only displays one MusicPlayer, the second one which is found, in the Level 1 scene is destroyed.

Now, if we look at what we are outputting to the console, we state how many objects of type MusicPlayer existing, and on Level 1 that is two (briefly), we destroy the second one and then get a new count and output that to the console, it still says two.

What hadn’t occurred to me earlier (when I suggested adding the extra debug statement) is that you make the call to destroy the second music player and output the new count in the same frame, but, and this is the important part, the GameObject isn’t destroyed until the end of that frame. So where we added the code to get the new count and output the value, at that point in time there still are two.

For the end of that frame, its unlikely that you’d notice any ill effects of it existing so very briefly, however, what you can do to work around this issue (the issue is only caused because of the way the singleton is being attempted), you can add this line;

gameObject.SetActive(false);

before the Destroy(gameObject); statement.

Not being active it will not be able to do anything, any Find methods will also not return it, if you run the game again now you’ll note that the console outputs that the number of MusicPlayer’s after action is now one.

Note, you have Collapse turned on in the console, so turn it off and you’ll see those messages appear in the order you’d expect them.

Hope this helps explain what is/was going on. Moving forward you could leave the statement in to deactivate the GameObject and just remove all of the Debug.Log statements, and that second Find statement, so you’d end up with this;

private void Awake()
{
    int numMusicPlayers = FindObjectsOfType<MusicPlayer>().Length;

    if (numMusicPlayers > 1)
    {
        gameObject.SetActive(false);
        Destroy(gameObject);
    }
    else
    {
        DontDestroyOnLoad(gameObject);
    }
}

Hope this helps :slight_smile:

Blimey that is a long message, didn’t expect that

Thank you for helping, that’s solved the issue. Just not sure why the problem happened in mine but not in the video when - as far as I can tell - my project was the same?

well, I mean, it doesn’t appear to happen in the video anyway

Anywho, thank you for taking the time.
I am certain I will have future issues lol

1 Like

I can’t recall, but if in the video you were only using the Hierarchy, not any console output of counters, then it would appear right.

In any case, you are very welcome and I hope you enjoy the rest of the course :slight_smile:


See also;

2 Likes

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

Privacy & Terms