5
votes

I using Unity 2019.2.14f1 to create a simple 3D game.

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).

The gameObject with the tag that I want to destroy has an Audio Source:

Audio Source on the GameObject with the specific tag


In order to make the sound playing on the collision, I started by creating 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.

On 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, 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, any guidance on how to make the sound playing after a collision between the player and the gameObject with the tag Present is appreciated.


Edit 1: Assuming that it is helpful, here it goes the full ThirdPersonUserControl.cs:

using System;
using UnityEngine;
using UnityEngine.UI;
using UnityStandardAssets.CrossPlatformInput;

namespace UnityStandardAssets.Characters.ThirdPerson
{
    [RequireComponent(typeof (ThirdPersonCharacter))]
    public class ThirdPersonUserControl : MonoBehaviour
    {

        public Text countText;
        public Text winText;

        private int count;
        private ThirdPersonCharacter m_Character; // A reference to the ThirdPersonCharacter on the object
        private Transform m_Cam;                  // A reference to the main camera in the scenes transform
        private Vector3 m_CamForward;             // The current forward direction of the camera
        private Vector3 m_Move;
        private bool m_Jump;                      // the world-relative desired move direction, calculated from the camForward and user input.


        private void Start()
        {

            count = 20;
            SetCountText();
            winText.text = "";

            // get the transform of the main camera
            if (Camera.main != null)
            {
                m_Cam = Camera.main.transform;
            }
            else
            {
                Debug.LogWarning(
                    "Warning: no main camera found. Third person character needs a Camera tagged \"MainCamera\", for camera-relative controls.", gameObject);
                // we use self-relative controls in this case, which probably isn't what the user wants, but hey, we warned them!
            }

            // get the third person character ( this should never be null due to require component )
            m_Character = GetComponent<ThirdPersonCharacter>();
        }


        private void Update()
        {
            if (!m_Jump)
            {
                m_Jump = CrossPlatformInputManager.GetButtonDown("Jump");
            }
        }


        // Fixed update is called in sync with physics
        private void FixedUpdate()
        {
            // read inputs
            float h = CrossPlatformInputManager.GetAxis("Horizontal");
            float v = CrossPlatformInputManager.GetAxis("Vertical");
            bool crouch = Input.GetKey(KeyCode.C);

            // calculate move direction to pass to character
            if (m_Cam != null)
            {
                // calculate camera relative direction to move:
                m_CamForward = Vector3.Scale(m_Cam.forward, new Vector3(1, 0, 1)).normalized;
                m_Move = v*m_CamForward + h*m_Cam.right;
            }
            else
            {
                // we use world-relative directions in the case of no main camera
                m_Move = v*Vector3.forward + h*Vector3.right;
            }
#if !MOBILE_INPUT
            // walk speed multiplier
            if (Input.GetKey(KeyCode.LeftShift)) m_Move *= 0.5f;
#endif

            // pass all parameters to the character control script
            m_Character.Move(m_Move, crouch, m_Jump);
            m_Jump = false;
        }

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

                //FindObjectOfType<AudioManager>().Play("CatchingPresent");
                AudioManager.instance.Play("CatchingPresent");
            }
        }

        void SetCountText()
        {
            countText.text = "Missing: " + count.ToString();
            if (count == 0)
            {
                winText.text = "You saved Christmas!";
            }
        }
    }
}

Edit 2: Hierarchy in Unity:

Hierarchy in Unity

2
First off, well done on a wonderfully comprehensive question. It's nice to see the effort put into this. Now to clarify, AudioManager.instance is not defined in the script snippet above; do you assign its value in Awake/Start? It is clearly defined so I feel that something else is stopping compilation. Does it compile when you remove those two lines of code? You can also use this script to create that singleton. So you can do AudioManager.Instance... What is the namespace that you call the collision code in? - user14492
@user14492 First, yes. Second question, yes. If it helps I can add the whole ThirdPersonUserControl.cs - Gonçalo Peres 龚燿禄
Sure if you don't mind sharing it. - user14492
@user14492 it is already after "Edit 1". - Gonçalo Peres 龚燿禄
Do a quick search to check there isn't another AudioManager class in the project part of standard assets? - user14492

2 Answers

1
votes

Reformulated the approach that I was following and solved the problem 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();
            }
        }
-1
votes

Importing your scripts myself works without any issues when using FindObjectOfType<AudioManager>().Play("CatchingPresent");. Try reimporting your scripts from the editor (right click in the project folder > reimport all. this might take a while depending on the size of your project)

to use AudioManager.instance.Play("CatchingPresent"); you would first need to create a static variable that holds instance like this (this only works as a singleton, and will break if multiple AudioManager's are in the scene):

public class AudioManager : MonoBehaviour
{
    //Create a static AudioManager that will hold the reference to this instance of AudioManager
    public static AudioManager Instance;
    public Sound[] sounds;

    //Assign Instance to the instance of this AudioManager in the constructor
    AudioManager()
    {
        Instance = this;
    }

    // Rest of the AudioManager code
}

Doing it like this, and using the rest of your code also works for me.