Audio Clips don't play

I’m having a problem with getting my “GameClip” and “EndClip” to play. My StartClip is the only one that plays no matter what scene I’m on. I don’t think that my OnLevelLoaded( ) is running at all. As you can see I have a Debug statement but it doesn’t log.

I have followed the code just as it is in the lecture. I’m running Unity 5.6.
What am I missing? If anyone could give me a little feedback it would greatly be appreciated. Thanks

I’ve changed the obsolete “OnLevelWasLoaded” part.

Hi Pan,

Before we start, it’s much easier if you copy/paste your code into your posts and then just apply the code formatting characters (see the link at the end of my reply), by doing so, you allow those who help you to more easily read your code and they can also copy/paste chunks of it back in a response if they have any suggestions, without perhaps having to type it all out. Screenshots are very useful however for errors messages within Unity or details from the Unity editor itself, perhaps Inspector details for example.

Ok so the problem in hand.

It looks like you’ve replaced the obsolete code with a suitable replacement. You are of course missing the checks for the additional levels, so at the moment you would only have the startClip playing.

You’re not currently calling music.Stop() or music.Play() or setting music.Loop = true either, other than in the Awake method. I would add a check around this too, for example;

if(music.isPlaying)
{
    music.Stop();
}

That way, should it not be for any reason, it won’t error and fall over.

Let me know if you are still having any problems.

After changing that obsolete code. I don’t know how to properly structure the logic. I’ve looked at other people’s examples and their suggestions but it just makes me more confused. I usually have to watch the lecture videos more than once and pick things apart to know what things do and how they are structured. But now I don’t know how much of the “old” code structure I can use.

Here is a screenshot of where I am sitting with my code, not too much different from my original post.

I forgot to add the link for you didn’t I - my apologies!


Regarding your question… the code is looking good, with regards to your OnSceneLoaded method, try this;

private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
	int level = SceneManager.GetActiveScene().buildIndex;

	if(music.isPlaying)
	{
		music.Stop();
	}

	if (level == 0)
	{
		music.clip = startClip;
	}
	else if (level == 1)
	{
		music.clip = gameClip;
	}
	else if (level == 2)
	{
		music.clip = endClip;
	}
	else
	{
		Debug.LogWarning("SceneManager.GetActiveScene().buildIndex not supported in method OnSceneLoaded.");	
	}

	if(music.clip)
	{
		music.Play();
		music.loop = true;
	}
}

Alternatively, you could use a switch statement;

private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
{
	int level = SceneManager.GetActiveScene().buildIndex;

	if(music.isPlaying)
	{
		music.Stop();
	}

	switch(level)
	{
		case 0 :
			music.clip = startClip;
			break;
		case 1:
			music.clip = gameClip;
			break;
		case 2:
			music.clip = endClip;
			break;
		default:
			Debug.LogWarning("SceneManager.GetActiveScene().buildIndex not supported in method OnSceneLoaded.");
			return;
	}

	if(music.clip)
	{
		music.Play();
		music.loop = true;
	}
}
1 Like

Yeah, I understand what you meant about copy/pasting and adding formatting characters vs a screenshot. thanks for the link.

I will pour over both of these examples. It looks like a lot of the code structure is still the same. I just wasn’t sure how to proceed. Thank you!

I have heard that the same obsolete code pops up in Glitch garden so now hopefully I’ll know how to handle it when I come to it.

1 Like

You’re very welcome.

If you check lecture 8, right at the beginning of the course, you should find a link to a Google Doc under Resources which covers the differences between the version of Unity which the course was originally using and the more recent versions.

In addition, the GameDev.tv team are currently in the process of updating the original content to support the more recent version(s) of Unity.

Hope this helps :slight_smile:

Please mark the topic solved when you feel it is by using the Solved functionality;

image

Ok, Thanks! I’ll check it out.

1 Like

It works! Thanks again Rob. You rock!

1 Like

You are very welcome :slight_smile:

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

Privacy & Terms