Sound is not working?

Hello, for me the sound is not working , and i cant find the isue.

Hi Tomas,

Rather than taking screenshots for code, please copy/paste your code into your post and then apply the code formatting characters. This helps to improve readability and also enables those who offer to help you the ability to copy/paste parts of your code back to you with suggestions or corrections.

Regarding the issue, pop a Debug.Log statement in on the first line of your OnCollisionEnter2D method, run the game, do you see this message appear in the console when the ball collides with something?


See also;

Here is my code
The message does appear in my console

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Ball : MonoBehaviour {

    //config params
    [SerializeField] Paddle paddle1;
    [SerializeField] float xPush = 2f;
    [SerializeField] float yPush = 15f;
    [SerializeField] AudioClip[] ballSounds;
    
    //state
    Vector2 paddletoballvector;
    bool HasStarted = false;

    // Cached component references
    AudioSource myAudioSource;

	// Use this for initialization
	void Start ()
    {
        paddletoballvector = transform.position - paddle1.transform.position;
        myAudioSource = GetComponent<AudioSource>();
	}
	
	// Update is called once per frame
	void Update ()
    {
        if(HasStarted==false)
        {
            LockBallToPadlle();
            LounchBallOnMouseClick();
        }
        
    }

    private void LounchBallOnMouseClick()
    {
        if(Input.GetMouseButtonDown(0))
        {
            GetComponent<Rigidbody2D>().velocity = new Vector2(xPush, yPush);
            HasStarted = true;
        }

    }

    private void LockBallToPadlle()
    {
        Vector2 paddlepos = new Vector2(paddle1.transform.position.x, paddle1.transform.position.y);
        transform.position = paddlepos + paddletoballvector;
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if(HasStarted)
        {
            AudioClip clip = ballSounds[UnityEngine.Random.Range(0, ballSounds.Length)];
            myAudioSource.PlayOneShot(clip);
        }
    }
}

Hi,

Ok, that’s good so we are detecting collisions.

Some more things to test…

  • Do you have any other audio in the game which is working?
  • Does your Main Camera have an Audio Listener component?
  • In the Game view, is the Mute Audio button pressed?

The last thought is that perhaps the audio effects distance from the Audio Listener could be the issue. The easiest way to test that would be to disable the Audio Listener on the Main Camera and then add an Audio Listener component to the Ball GameObject, as it is creating the sounds there should be no distance in between, again, just to test.

Failing this, if you want to share your project files with me I will happily take a look for you.

The forum will allow uploads of up to 10MB, if your project files (zipped) are larger than that you would need to use a service such as Google Drive or Dropbox, and then share the URL.

Hi.
The Mute Audio button was predded…:joy: thank you for the help and effort!!!

1 Like

Hi Tomas,

Great - an easy fix then - you’ll never forget that now, deep learning :nerd_face:

You’re very welcome and I’m glad you can move forward again :slight_smile:

I have been think about the sound couple a days… and all I had to do is unpress the button​:smile::smile::relieved:

1 Like

hehe, I spent 4 hours once trying to work out why my game’s score wasn’t appearing in the UI Text object in the Canvas… back and forth I went, checking code, double-checking code - and then I realised that it was there, it was just too long for the length of the UI Text object so it wrapped around and effectively was underneath…

…oh how I laughed… not…

You will gain many more of these little stories as you continue your journey - they are a great way to learn :slight_smile:

1 Like

I have one more question, so I think I will ask it right now😂.
Mm why are we making all the game in size of Main Camera/Game camera but not by the size of canvas. Hope so my question makes sense🤣

The canvas will be effectively re-sized to fill the size of the camera. The fact that the canvas shows as being much bigger within the editor isn’t an issue. The default 1 unit corresponds to 1 pixel in Scene view.

Oh okeyy!! Thank you once again

1 Like

No problem :slight_smile:

1 Like

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

Privacy & Terms