Multiple sounds that can be activated, code doesn't work?

I tried this code so multiple sounds can be activated in the same scene but in different locations:

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

public class AudioCollider : MonoBehaviour
{
    public GameObject audioObject;
   
    // Start is called before the first frame update
    void Start()
    {
        audioObject = GameObject.FindGameObjectWithTag("Audio");
    }

    void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag ("Player"))
        {
            audioObject.SetActive(true);
            this.gameObject.SetActive(true);

        }
    }
}

This is my setup for the code:

It doesn’t work, why?

Hi,

The first issue is you can’t find inactive game objects. As the Audio objects are all set to inactive at the start they will never get assigned. An alternative would be calling Play() on the Event Emitter instead. All event emitter functions can be found under FMOD Unity | Scripting API Reference.
Your new code would look like this:

public class AudioCollider : MonoBehaviour
{
    public FMODUnity.StudioEventEmitter audioObject;
   
    // Start is called before the first frame update
    void Start()
    {
        audioObject = GameObject.FindGameObjectWithTag("Audio");
    }

    void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag ("Player"))
        {
            audioObject.Play();
            this.gameObject.SetActive(true);
        }
    }
}

The added benefit is you have access to all the functions attached to the Event Emitter.
Now you will want to set all your Audio objects to Active in the scene and make sure their Play Event option is set to none:
image
You can leave Stop Event to destroy to make sure memory is cleaned up properly.
Are there any other errors being logged in the console window?
image
The console window is very useful too for debugging.

Hope this helps!

1 Like

thankyou ! that helps a lot!!
it does give an error, i’ll try to find a way to fix it.

eitherway if anyone feels interested to find a solution: what the error is saying is that the Event Emitter is not a GameObject and it can not be assigned like such.

here’s my solution:

> using System.Collections;
> using System.Collections.Generic;
> using UnityEngine;
> 
> public class AudioCollider : MonoBehaviour
> {
>     public GameObject audioObject;
>     public FMODUnity.StudioEventEmitter playingaudio;
> 
>     // Start is called before the first frame update
>     void Start()
>     {
>         audioObject = GameObject.FindGameObjectWithTag("Audio");
>     }
> 
>     void OnTriggerEnter(Collider other)
>     {
>         if (other.CompareTag("Player"))
>         {
>             playingaudio.Play();
>         }
>     }
> 
>     void OnTriggerExit(Collider other)
>     {
>         if (other.CompareTag("Player"))
>         {
>             playingaudio.Stop();
>         }
>     }
> }

Apologies, to solve the error

You could do something like

public class AudioCollider : MonoBehaviour
{
    public FMODUnity.StudioEventEmitter audioPlaying;
    public GameObject audioObject;
   
    // Start is called before the first frame update
    void Start()
    {
        audioObject = GameObject.FindGameObjectWithTag("Audio");
        audioPlaying = audioObject.GetComponent<FMODUnity.StudioEmitter>();
    }

Hope this helps!

1 Like

It’s ok! The other thing already helped a lot but this helps even more.

1 Like