- Home /
Audio isn't playing for some reason
So I have this code and for some reason, the audio won't play. Everything is setup correctly I believe. When I enter the game a split second of the water clip plays for some reason and stops but then when I go underwater, I hear no sound? Any help?
function Update () {
if (transform.position.y < underwaterLevel) {
RenderSettings.fog = true;
RenderSettings.fogColor = Color (0, 0.4, 0.7, 0.6);
RenderSettings.fogDensity = 0.04;
underWaterSound.Play();
// RenderSettings.skybox = noSkybox;
}
else {
RenderSettings.fog = defaultFog;
RenderSettings.fogColor = defaultFogColor;
RenderSettings.fogDensity = defaultFogDensity;
underWaterSound.Stop();
// RenderSettings.skybox = defaultSkybox;
}
}
Throw this in your update function and see what kind of values you are getting.
Debug.Log(transform.position.y + " < " + underwaterLevel);
Answer by VesuvianPrime · Feb 14, 2014 at 04:44 PM
I suspect you probably shouldn't be telling the underWaterSound to play on Update. Instead check to see if the "underwater" state has changed:
private bool m_UnderWater = false;
void Update()
{
if (transform.position.y < underwaterLevel)
this._setUnderWater(true);
else
this._setUnderWater(false);
}
void _setUnderWater(bool state)
{
if (state == m_UnderWater)
return;
m_UnderWater = state;
if (m_UnderWater)
underWaterSound.Play();
else
underWaterSound.Stop();
}
hmmm ok I'll try that! Thanks! Still sort of learning Javascript as I go along
I'm having troubles converting this into javascript, $$anonymous$$d lending me a hand? :)
I don't work in Javascript, but you're more than welcome to add me on Skype (VesuvianPrime)
Your answer
![](https://koobas.hobune.stream/wayback/20220613133513im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Audio Listener not working. 1 Answer
Footsteps Sound Effect causing other Audio Clipping or no sound at all. 1 Answer
How to change priority of audio clips 2 Answers
Play Audio Specific Speakers 0 Answers
Record in game Audio on android 2 Answers