Sound effects

Im trying to add sound effect to my game i made a soundmanager and gave it 3 audio sources then i made this script

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

public class SoundManager : MonoBehaviour
{
// public

public AudioSource DeathSound;
public AudioSource ScoreSound;
public AudioSource Powerup;

// void

public void DeathS() 
{
DeathSound.Play();
}

public void ScoreSoundS() 
{
ScoreSound.Play();
}

public void PowerupS() 
{
Powerup.Play();
}

}

How would i implement it into my p[layer script so that when he dies it will play this i my playerscript

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

public class Player : MonoBehaviour
{
    // public

    public TimeManager timeManager;

    public float force = 5f;

    public Rigidbody2D rb;

    public bool isGrounded;

    // private

    GameObject playerPrefab;

    private Vector2 startSwipe;
    private Vector2 endSwipe;

    // void

    void Start()
    {
        playerPrefab = GameObject.FindGameObjectWithTag("Player");
    }

    void Update()
    {
        // movement
        if (Input.GetMouseButtonDown(0))
        {
            startSwipe = Camera.main.ScreenToViewportPoint(Input.mousePosition);
        }

        if (Input.GetMouseButtonUp(0))
        {
            endSwipe = Camera.main.ScreenToViewportPoint(Input.mousePosition);
            Swipe();
        }

        // Slo-mo, Squeeze & health

        if (Input.GetMouseButtonDown(0) && isGrounded == false)
        {
            timeManager.DoSlowMotion();
            Squeeze();
            GetComponent<PlayerHealth>().numOfHearts --;
        }else if (Input.GetMouseButtonUp(0) && isGrounded == false)
        {
            UnSqueeze();
        }
    }

    void Swipe()
    {
        Vector2 swipe = endSwipe - startSwipe;

        rb.AddForce(swipe * force, ForceMode2D.Impulse);
    }

    void Squeeze()
    {
        playerPrefab.transform.localScale = new Vector3(0.45f, 0.5f, 1f);
    }

    public void UnSqueeze()
    {
        playerPrefab.transform.localScale = new Vector3(0.5f, 0.5f, 1f);
    }

}

Where in your script does the Player die? That’s where you would invoke the soundmanager’s DeathS method.

Two comments:

  • You should only use one AudioSource and use AudioClips
  • It yould help if you reference the SoundManager in your Player class

Ok but what should i write where my player dies cause when i write
GetComponenet<SoundManager>().DeathS(); it doesn’t play anything

Try the following

SoundManager theSM = GetComponent<SoundManager>();
if (theSM) {
    theSM.DeathS();
} else {
   Debug.Log("Did not find the Sound Manager");
}

And run it to make sure that the script correctly locates your Sound Manager. Then make sure that the PlayOnStart is UNchecked for each AudioSource and the Volume is set to 1.0. Also, disable any 3D Sound, to make sure you can actually hear the Sound no matter how distant the audiolistener, and make sure there is an Audio listener in the Scene (it’s usually attached to the camera)

Ye i did that but it can’t find the soundmanager what should i do (sorry i,m new to coding

Make sure the soundmanager is attached to the same object as the Player script that is invoking GetComponent()

Thanks that got it working