Forum Discussion

🚨 This forum is archived and read-only. To submit a forum post, please visit our new Developer Forum. 🚨
JaggerWolf's avatar
JaggerWolf
Expert Protege
11 years ago

Need Audio Help In Unity!!

I have been designing a game level in Unity 4 - That part was easy....Now I am onto adding sounds for the level. I suck at scripts, still haven't got the hang of it. I am looking for a way to trigger a sound that is on an object away from the player - I am guessing I would need two scripts one for the trigger and one on the object where the sound is.

Example: Player walks through trigger - The sound plays behind them, Or - Player enters trigger - sound turns on the radio on the shelf.

I have been able to make a trigger with a sound attached, but have been unable to trigger a sound on an object that is a distance from the player.

Any help would be great - I have asked on unity forums but there hasn't been much help.

Thanks!!

2 Replies

Replies have been turned off for this discussion
  • I think you want to make a reference to that object with the sound from your trigger script to play the sound. something like
    Gameobject the_radio;
    the_radio = Gameobject.Find("the_radio_gameobject");
    the_radio.audio.Play();
  • bilago's avatar
    bilago
    Honored Guest
    "JaggerWolf" wrote:
    I have been designing a game level in Unity 4 - That part was easy....Now I am onto adding sounds for the level. I suck at scripts, still haven't got the hang of it. I am looking for a way to trigger a sound that is on an object away from the player - I am guessing I would need two scripts one for the trigger and one on the object where the sound is.

    Example: Player walks through trigger - The sound plays behind them, Or - Player enters trigger - sound turns on the radio on the shelf.

    I have been able to make a trigger with a sound attached, but have been unable to trigger a sound on an object that is a distance from the player.

    Any help would be great - I have asked on unity forums but there hasn't been much help.

    Thanks!!



    This is how I manage my sound effects:

    /// <summary>
    /// Play an Effect from a GameObject with an audiosource
    /// </summary>
    /// <param name="sourceObject">Object with AudioSource</param>
    /// <param name="clip">Clip you want to play</param>
    /// <param name="multiplier">Percent of max volume do you want this to play at (ie 0.5f = 50%)</param>
    public void playEffect(GameObject sourceObject, AudioClip clip, float multiplier)
    {
    if (sourceObject.GetComponent<AudioSource>() != null)
    {
    sourceObject.GetComponent<AudioSource>().PlayOneShot(clip, masterVolume_Effects * multiplier);
    Debug.Log(string.Format("Sound {0} has been played by Object {1}", clip.name, sourceObject.name));
    }
    else
    Debug.Log(string.Format("Object {0} does not have an audio source!", sourceObject.name));
    }


    Put that in your script, and ontriggerEnter() you can then call

    playEffect(this.gameObject,clipToPlay,0.5f);