Audio Not Working Immediately On Glitch Garden (when on ShareMyGame)

Hi everyone,

I’ve now made three different games through this course and Block Breaker and the Laser Defense game both played music properly when loading to Simmer / ShareMyGame. The Glitch Garden game doesn’t start playing music upon load until the user clicks into the actual game itself (doesn’t start playing upon load). Can anyone tell me why that might be?

Thanks!
Tim

Hi,

Please follow Rob’s instruction in this thread. Maybe there is a helpful error message in your browser console.

Alright… followed Rob’s instructions and found this in the console:

The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page. https://goo.gl/7K7WLu

So that explains the reasoning behind what’s happening but I am not sure why my other games didn’t see this issue. What’s weird is I just loaded my Laser Defender game (Narwhals vs Aliens) and it had the same issue but previously, when I was troubleshooting, it didn’t. Guessing maybe it’s an issue with ShareMyGame maybe unless you or someone else has an idea.

The bummer is that I spent hours trying to figure this out on Friday and even restructured my game to try to avoid the issue when it turns out it was most likely not my game’s fault.

Any other suggestions on how to enable audio immediately upon load?

Thanks for all the help!

I’ve tested your game again, and this time, a NullReferenceException popped up.

Unfortunately, the error message you discovered doesn’t tell me anything but I took a look at the browser console and found this:

image

Check the GetMasterVolume method and compare it with Rick’s code. Maybe it makes sense to pass on a second argument, e.g. 1f, to the GetInt method.


The following doesn’t have anything to do with the error message but try to remove all your scenes from the Build Settings list and readd them. Then build your game again. In some cases, the Build Settings are buggy and cause “funny” issues.

Hey Nina… not 100% sure what you mean by “it makes sense to pass a second argument”. I don’t believe we have a GetInt method anywhere as we’re using all floats. I did go through and make sure my code matched Rick’s. It was close but not perfect. I also set it back so that it would go splash screen to start menu. When the game loads for the very first time, however, it will not play the main music until I go into options (it does play the splash screen welcome effect assuming the user has clicked on the game viewer). I do not get the error above anymore when going into or adjusting any options.

Did I miss something in the lecture? Should we be checking to see if there are playerprefs already defined? It appears that mine are set to 0 for both difficulty and for volume on the initial load. If there are none defined, should we be setting defaults? As far as I can tell, I have implemented pretty much everything that Rick had. Once I go in and set the music volume and difficulty, they stick fine.

Thanks for all your help.

In that case, pass on a second argument to the GetFloat method. That would be the default value. For example:

PlayerPrefs.GetFloat("Your key", 1f);

You didn’t miss anything. If you start the game for the first time, GetFloat returns 0f because the key does not exist in the PlayerPrefs yet. By setting your own default value, you will not get the problem of hearing no sound when starting the game for the first time.

Since Rick has started his game many times, his PlayerPrefs already have the key he’s trying to access, so the GetFloat method doesn’t return 0f, and he doesn’t encounter the problem.

It might be that the PlayerPrefs do not persist in WebGL games. Which would be an explanation for the missing(?) sound. Enable the debug mode and add Debug.Logs to your code to see what’s going on during runtime in your build. Since you cannot see sound clips or components, there is no way to tell whether the clip is missing or if the AudioSource is muted just by staring at the rendered picture on a screen.

(Debugging is all about verifying or falsifying theories.)

Thanks for the advice and the information about the second argument. This is all about learning and that’s super helpful. I’ve added those in for both audio and the difficulty.

I think there’s something potentially with ShareMyGame as the audio actually played for my game right out of the gate today.

I’m going to leave this as is for now, I think, and move on to the other tutorials for the platformer!

This topic was automatically closed after 2 days. New replies are no longer allowed.

Privacy & Terms