Unity3D Playing Sound when Player Collides with an Object with a specific tag

2 posts / 0 new
Last post
goncaloperes
goncaloperes's picture
Unity3D Playing Sound when Player Collides with an Object with a specific tag

Hey everyone! smiley

 

I using Unity 2019.2.14f1 to create a simple 3D game, as you know from previous posts.

In that game, I want to play a sound anytime my Player collides with a gameObject with a specific tag.

The MainCamera has an Audio Listener and I am using Cinemachine Free Look, that is following my avatar, inside the ThridPersonController (I am using the one that comes on Standard Assets - but I have hidden Ethan and added my own character/avatar).

For the collisions, I accessed the Third Person Character script, in order to destroy a gameObject (with a specific tag) when colliding with it, I have added the following:

void OnCollisionEnter(Collision other)
        {
            if (other.gameObject.CompareTag("Present"))
            {
                Destroy(other.gameObject);
                count = count - 1;
                SetCountText();

            }
        }

It is working properly as that specific object is disappearing on collision.

Now, for the Audio, I see two possible ways of making it work.


The first one, by simply adding an Audio Source to the ThirdPersonController (with the AudioClip that I wanted to call) and added GetComponent<AudioSource>().Play(); to the if statement as it follows:

void OnCollisionEnter(Collision other)
        {
            if (other.gameObject.CompareTag("Present"))
            {
                Destroy(other.gameObject);
                count = count - 1;
                SetCountText();
                GetComponent<AudioSource>().Play();
            }
        }

Does the work and it plays the sound when the player collides with the gameObject with the tag "Present".


Another way, and it is the one that I intend to make it work as it is more versatile (and can work nicely with a variety of AudioSources), is the following:

Start by adding a gameObject with the tag that I want to destroy has an Audio Source:

Audio Source on the GameObject with the specific tag

Then, created an empty gameObject to serve as the AudioManager and added a new component (C# script) to it:

using UnityEngine.Audio;
using System;
using UnityEngine;

public class AudioManager : MonoBehaviour
{

    public Sound[] sounds;

    // Start is called before the first frame update
    void Awake()
    {
        foreach (Sound s in sounds)
        {
            s.source = gameObject.AddComponent<AudioSource>();
            s.source.clip = s.clip;

            s.source.volume = s.volume;
            s.source.pitch = s.pitch;
        }
    }

    // Update is called once per frame
    public void Play (string name)
    {
        Sound s = Array.Find(sounds, sound => sound.name == name);
        s.source.Play();
    }
}

And created the script Sound.cs:

using UnityEngine.Audio;
using UnityEngine;

[System.Serializable]
public class Sound
{
    public string name;

    public AudioClip clip;

    [Range(0f, 1f)]
    public float volume;
    [Range(.1f, 3f)]
    public float pitch;

    [HideInInspector]
    public AudioSource source;
}

After that, in the Unity UI, I went to the Inspector in the gameObject AudioManager, and added a new element in the script that I named: CatchingPresent.

AudioManager - Sound that I want to play when the player collides with an object.

Now, in order to play the sound "CatchingPresent" anytime the Player collides with the object with the tag, in this case, Present, I have tried adding the following to the if in the OnCollisionEnter:

  • FindObjectOfType<AudioManager>().Play("CatchingPresent");

But I get the error:

The type or namespace name 'AudioManager' could not be found (are you missing a using directive or an assembly reference?)

  • AudioManager.instance.Play("CatchingPresent");

But I get the error:

The name 'AudioManager' does not exist in the current context

As all the compiler errors need to be fixed before entering the Playmode, I now need to fix one of these two problems.

I already checked and there isn't another AudioManager class.

I tried reimporting my scripts from the editor (right click in the project folder > Reimport All).

The problem persists, so any guidance on how to make the sound playing after a collision between the player and the gameObject with the tag Present is appreciated.

 

Best regards,

Gonçalo Peres

Visibility: 
Public - accessible to all site users
Edited by: goncaloperes on 2019/12/11 - 19:59
tester
tester's picture
Hi,

Hi,

1-First you should not change the standard " Third Person Character script ". Create a new script with the desired behaviour and add it to the Player.

2-If unity says that "something ... could not be found " you probably need to define it before using.

Be well