Audio not working right after "else audioSource.Stop"

I added this exactly as Ben did toward the end of part 47, and now the audio doesn’t start and end as it should. Before I added the ‘else’ statement, the sound was correctly triggered when pressing the spacebar. Now, when I play the game and press the spacebar, the sound doesn’t seem to trigger – it sometimes does, but only after holding down the spacebar for several seconds. Sometimes it will finally trigger, then continue to play for several seconds after I stop pressing the spacebar. There isn’t any consistency in how it’s working. I try pressing and unpressing the spacebar, and it still doesn’t work. At first I thought maybe my system was being a bit laggy but I don’t believe that’s the case. The sound file isn’t very large either. Any ideas?

 if (Input.GetKey(KeyCode.Space))
        {
            rigidBody.AddRelativeForce(Vector3.up);
            if (!audioSource.isPlaying)
            {
                audioSource.Play();
            }
            else
            {
                audioSource.Stop();
            }
1 Like

Hi Neb.

One of the best first steps to troubleshooting most issues is adding a Debug.Log statement to the if and else statements to see what is applying and when. e.g.

if (Input.GetKey(KeyCode.Space))
{
	Debug.Log("Space press registered. Applying force."
	rigidBody.AddRelativeForce(Vector3.up);
	if (!audioSource.isPlaying)
	{
		Debug.Log("Audiosource detected as not playing. Playing audioSource."
		audioSource.Play();
	}
	else
	{
		Debug.Log("Audiosource detected as playing. Stopping audioSource."
		audioSource.Stop();
	}
}

With that said, I think it’ll turn out to be a flaw in the code provided at the time which gets addressed in a later video. This happens as the lecturers introduce new concepts building on previous lessons. Just like a coder in the real world changing out code that they realise can be done better, Ben does so throughout the course.

In this case I think the Play() call gets swapped for a PlayOneShot call later but that concept would not be fully appreciated without first knowing Play(), it’s nuances and where it can fall down. Check out the forum for others that ran into your issue. I believe I did at this point to.

I hope that helps.

3 Likes

Thank you, I will keep at it, I’m still a real beginner when it comes to code but I’m sure things will get clearer as I go through the lessons etc.

1 Like

I made the same mistake myself …
Look better:
“else” is below and at the same level as " if (Input.GetKey (KeyCode.Space)) "
and not at the same level as " if (! audioSource.isPlaying) "
We are beginners, we will learn …:wink:

3 Likes

Hey Thanks… THIS SINGLE LINE HELPED ME SOLVE THE SAME ISSUE. It just didn’t ring the bell earlier that it is below the (Input.GetKey (KeyCode.Space)) part and not independent. Thank you again :slight_smile:

1 Like

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

Privacy & Terms