I need help with audio in my game

so i recently started trying gamedev again, this time trying to learn and read instead of just following tutorials 24/7. the only tutorial i followed was “The Unity Tutorial For Complete Beginners” from “Game Maker’s Toolkit” and i wanted to add audio to the game but i cant figure out how. In that video he teaches how make a flappy bird game, i wanted to try and add audio to when you get a point but i cant understand what im doing wrong

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class LogicScript : MonoBehaviour
{
    public int playerScore;
    public Text scoreText;
    public GameObject GameOver;
    public AudioSource pickupCoin; //pickupCoin is the audio i wanna use

    private void Start()
    {
        pickupCoin = GetComponent<AudioSource>();
    }

    [ContextMenu("Increse Score")]
    public void addScore(int scoreToAdd)
    {
        playerScore = playerScore + scoreToAdd;
       scoreText.text = playerScore.ToString();
        pickupCoin.Play();
    }


    public void restartGame()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().name);
    }


    public void gameOver()
    {
        GameOver.SetActive(true);
    }

}

i also read this page

Hey @Mendonca_Games !

Welcome to Unity :wink:

I imagine you might have solved your issue by now, but here’s a quick rundown of things to keep in might in Unity Audio.

In general, you will only need AudioClip and AudioSource.

  • AudioClip represents the original audio asset, the source data of the sound that you want to play. It’s where you will configure things such as loading policy, encoding format, etc.
  • AudioSource represents the location in space and the way you want the sound to be perceived. This component will allow you to set the sound volume and add effects.

In Unity, you simply have to put an AudioClip in an AudioSource, and hitting AudioSource.Play() should do the trick if your volume is higher than 0 and your scene contains an ‘AudioListener’.

If you have no sound at all, keep an eye on that button here just in case.

Or that checkbox here in ProjectSettings/Audio

Another very good thing to keep in mind is “2D sounds vs 3D sounds”.

  • A 2D sound is a sound played without “spatialization”, in the sense that it will be played straight as it is on the audio file. This is generally used for music and ambiance.
  • A 3D sound is, you probably guessed, “spatialized” and will be panned and filtered to create the impression of distance and direction, based on the AudioListener position. If your game is about a character evolving in space, that contributes A LOT to the immersion of the player.

Just that can get you quite far :slight_smile:

Feel free to reach out if you have more questions!
Cheers