About 'GetComponent To Play Audio'

Discussion for the lecture of ‘GetComponent To Play Audio’…

[Question] Hello!
I have a problem with Unity Audio - I can’t get it to play the sound ( except for ‘Play On Awake’ in the Component for Audio under ‘Ball’). As you can see from my screenshot the audio is set to -74.8DB . I can play the audio files and hear them no problem. Windows audio is set up and I have included Unity in the setup dialogue under configuration. I tried to add an audio mixer in Unity too… but to no avail! Any ideas and thanks for your help!
Eugene

Hm. To understand this :slight_smile:
This is the Part of the BLockout in the 2D Course ? i am right ?

We do not use “audioSource” we use “playClipAtPoint”

So you define your Audiofile not with the “.GetComponent<AUdioSource” in this case

you use “playClipAtPoint”

for this, you define in the above Stats, the audio Clip like:
private AudioClip ballbingg; ( or like you want to name it )

Later you do : AudioSource.PlayClipAtPoint so your Ball should be have the Audio Clip addet.

Will a lot more helpful to see cour Code. Best Greetings.

PS. you can also define a Volume there. So like:
public float Volume = 1f; ( all other is not needet, 1 is max)

Than you can add: playClipAtPoint(clip. ( here Position )) ,volume;

That doe it more intensive or lesser. You want to use audiosource “direct” i think this can be the Error.

Unity shows the current volume level. If there is no audio clip that is currently playing then it will always show “-74.8 dB”. Can you show us your code where you try to play the clip?

Also try this:
audioSource.PlayOneShot(clip, 1.0F);

audioSource is the reference to your AudioSource component on the ball game object
clip is the reference to your audio clip that should play
1.0F is the volume level at which the audio clip should play
(1.0F = 100%)

Thank you for your help and replies

Here is the script … I followed along as in the lecture " 62. Get Component to Play Audio".
However, as suggested I declared a Public float Volume = 1.0f;

For info, when I restart Unity with Ballbracker I get this attention / warning remark : "FMOD failed to set the software format to the custom sample rate 1, trying software fallback rate 48000 "

Look at line 60, it should be ‘OnCollisionEnter2D’. It is not a custom method that you define there, it is a predefined method in Unity (just like Start and Update) and if you don’t type it correctly (case-sensitive!) then Unity won’t be able to find it and call it when a collision occurs. Once you type it correctly its name should turn blue (again, just like Start and Update).



Considering the warning:
I was able to reproduce it:




Did you make changes to any audio settings (inside of Unity)?
Maybe you changed some values in the AudioManager (the value for ‘System Sample Rate’ should be at 0 by default) or in the import settings of your sound files?
Or maybe there is a script that messes with the audio settings?

i wonder what he is doing there ?
i am to far away from Screen this time, but did i read right ?

[SerializeField] ? That is not known in this Lessions.
Ben uses clearly following

using System.Collections;

using System.Collections.Generic;
using UnityEngine;

public class Ball : MonoBehaviour {

public bool hasStarted = false;            
private AudioClip ball;   

public int Health = 5;  // that is from my Code, not relevant here

private Paddle paddle;    
private Vector3 paddleToBallVector;

Later than:

private void OnCollisionEnter2D(Collision2D collision) {
	if (hasStarted) {
		AudioSource ballsound = this.gameObject.GetComponent<AudioSource>();
		ballsound.Play();

And again me to : I open your Picture in my Paint Programm to read it :smiley:WHy do you define a “Random.Range” for the Soundclip ? That is absolutly … not what you want!

Really:
following Things:

    • if you do the Course, follow the Course.
      There not inside (This time) a:
      [Serialize Field] <- never Teached @ Ben in this Lektions.
      Than your Script have complete other CHarakther, than Ben it use. Also you should use.
      AudioSource ballsound = this.gameObject.GetComponent();
      ballsound.Play();

and do not define a 2D sound, as a RandomRange with a component of 8 … this is not senceful. Because. if you define a Volume, and you give a randomrange on it, so unity have a free Choice from 0 up to 1 ( like i understand this here )
so if you do this, it is possible that your sound is silent, or you never hear anythng.

IF you rewrote in scripts, than ask if you do not understand what you doing, OR:
just do the Scripts like Ben it explains, because. The Course is fantastic, and: there is no need to do “big program Things” you not understand @ this time, if you wonder after this, why they will not work like you think.

A hint:
my Blockout uses “SerializeField” but i use it for Respawn, and for spawn Projectiles. For a Audio Clip you not need to serialize. This is just a public or a private Audioclip.

And remove the random Range, this cant work like this.

*Hm+ i do not see the Problem, why he needs to serialize Fields. Now i go back to the Course, and watch, and it is like it was, so nothing have changed ?

i hope that helps you.

@TheNightFlier
I believe you are watching the old course (section 7) where Ben indeed doesn’t use [SerializeField]. I think @Eugene84 is doing the new/updated course (section 5!).


(as you can see, there is a “[SerializeField]” in the new course)


That Random.Range part selects a random sound from the ballSounds array. It does not modify the volume in any way. This part is also from the new/updated course.


The problem here is that they misspelled ‘OnCollisionEnter2D’. That’s why Unity can’t find the method and does not call it when a collision occurs. That also means that the “play the sound” part is never reached and that’s why there is no sound.

1 Like

My tiny, tiny solution to the challenge:

private void OnCollisionEnter2D(Collision2D collision)
{
    if (hasStarted)
    {
        GetComponent<AudioSource>().Play();
    }
}
1 Like

is there a new Part / a New Video Material ?
I was in the Course, and watch the part, because: to see if Ben have changed something, but dont.
i watch @ Udemy ?

is this changed ? because i see the Content i wrote ?

Yes, Ben and Rick are updating the course. They are using the new Unity 2018 engine in the updated part. Here’s the progress so far (7/30/2018):

You are probably watching the old ‘Block Breaker’ videos (the section after ‘Updates And Important Messages’).

I have asked it in the Q&A, but after looking the code and searching a little, I think this “discussion” is a better place to ask it.

When the ball goes through the “LoseCollider”, the script of the ball tries to play the sound with the camera listener, and the script of the “LoseCollider” load the scene “GameOver”, which has another listener in its camera. So, the console warns:

Error mine, sorry!

[Answered] Hello All and thank you very much for your suggestions and help.
I resolved the issue by copying and comparing part of the code on GitHub (Ball.sc) and effectively there was a problem with my line - private void OnCollisionEnter2D(Collision2D collision) -

For info, I believe I am using the updated version of this course (Unity 2018) and Rick uses [SerializeField] .

1 Like

Privacy & Terms