Element 0

Oh right, here’s the link, hopefully it works for you.

https://drive.google.com/file/d/0B7SImL8yR1VJdTJ0RnMyeW5pTjA/view?usp=sharing

Yep… downloading…


Updated Thu Sep 14 2017 19:56

Sorry for the delay @recable, I have to mess around with Visual Studio as it was the first time I’d opened it since rebuilding my laptop over the last couple of days.

Ok… so running the project I get two nullreferenceexception errors.

What is happening is that OnEnable is calling the OnSceneLoaded method and this is happening before the Start method is called. As such, the audioSource object hasn’t been initialised by the time it is then used in the OnSceneLoaded method, where the audio clip is set.

The solution to this is to move the following line;

audioSource = gameObject.GetComponent<AudioSource>();

from the Start method and place it in the Awake method.

Note - the two errors relate to the same issue in both the code example I provided and in your original MusicManager.cs (line 31).

Interesting I have, on my mobile, a copy of my version of this game from a long time back and it does play the music on the splash screen, so I’m wondering if I spotted this, corrected it and then built/moved to mobile but somehow managed to not commit the changes to my source control.

In any case, this will resolve the issue for you and again, my apologies for the delay.

Thanks for your help, it does work now perfectly, however, can I have a bit of an explanation on why you need to have this SceneManager.sceneLoaded += OnSceneLoaded;

Edit: Sorry for the late reply, was away for a bit.

1 Like

Hey, sure, no problem.

So, the SceneManager sends a message to say “hey, the scene has loaded”, and this message is sent to all of the delegates which have been added via onSceneLoaded.

If you don’t add your delegate, you won’t receive this message.

To demonstrate, now that you have it working, comment out the line of code in question and you should find that the audio clip no longer plays, because the OnSceneLoaded method is never sent the message from the SceneManager.

Hope this helps.

Okay, thanks, so what’s with the += and -= on the OnEnable/Disable doing to the scenes?

Edit: Also can you explain why we need to reverse what’s happend in OnDisable?

1 Like

Of course, sorry, I should have considered that for you previously. The += and -= are called operators.

+= is short hand for saying “what it is, plus this”
-= is short hand for saying “what is is, minus this”

So, in our case we are saying;

SceneManager.sceneLoaded = SceneManager. sceneLoaded + OnSceneLoaded

Regarding the OnDisable, primarily to prevent any memory leaks. Where ever possible, tidy up after yourself, or pool objects and re-use them.


See also;

Sorry my bad, I understand what the += and -= I meant but I don’t know what we’re adding and taking away from the OnSceneLoaded? I hope you understand what I mean. :slight_smile:

We are adding / removing the delegate. :slight_smile:

Oh right, I need to look up more about a delegate, you also said it will be explained later in the course so I think I’ll leave my Music Manager the way you did it, and then come back to it and look again when delegates come up and see if I understand it then thanks for all the help, I’ve been a pain today. :slight_smile:

1 Like

There is this Unity tutorial also which may be of use;

I just started watching it, and it said you make a delegate by using the keyword delegate, however, this isn’t the case in the Music Manager, why is that?

The delegate declaration would be within the SceneManager class, we don’t really get to see this because we are simply using the classes in the way that they are exposed to us via Unity.

Privacy & Terms