Home>

I'm new to Unity.
I am making a 2D breakout game in Unity.
I want to make a sound effect when the Ball hits the Block and Bar.
The following source code script is attached to Ball.

There is no sound effect.

Corresponding source code

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

public class SoundController: MonoBehaviour
{
public AudioClip sound1;
public AudioClip sound2;

private AudioSource audioSource;

// Start is called before the first frame update
void Start ()
{
audioSource = GetComponent ();

}

// Update is called once per frame
void Update ()
{

}

private void OnCollisionEnter2D (Collision2D collision)
{
if (collision.gameObject.tag == "Bar")
{
audioSource.PlayOneShot (sound1);
}

if (collision.gameObject.tag == "Block")
{
audioSource.PlayOneShot (sound2);
}
}
}

What I tried

-If the content of if of OnCollision is set to Debug.Log, characters will be displayed without any problem = I think that there is no problem with collision detection.
-Moving the Audio Listener from the Main Camera to Ball had no effect.
-If i play the sound in the Project window, it can be played without any problem and the sound will be heard.
-Spatial Blend is also 2D.

Supplementary information (FW/tool version, etc.)

The version of Unity is 2019.4.10ver.

  • Answer # 1

    What's wrong? Is it the recognition that there is no sound even if you execute it?
    If you get a collision, what about, for example, the Editor is muted?

    There is a [Mute Audio], so please check it.
    https://baba-s.hatenablog.com/entry/2017/12/28/205400