- Home /
Play Sound on trigger
Hey i need to play a sound when i enter the trigger before the scrtipt destroy the gameobject but i cant figure out how, heres my script
using UnityEngine; using System.Collections;
public class Pickup : MonoBehaviour { public enum Item { Flashlight, Battery }
public Item item;
void OnTriggerEnter()
{
if(item == Item.Flashlight)
{
HUD.HasFlashlight = true;
}
else
HUD.BatteryCount++;
Destroy(gameObject);
}
}
Answer by CypherGames · Aug 18, 2013 at 01:26 PM
Try this, StickyDream:
using UnityEngine;
using System.Collections;
public class Pickup : MonoBehaviour {
public enum Item { Flashlight, Battery }
public Item item;
public AudioClip flashlightSound;
public AudioClip batterySound;
void OnTriggerEnter()
{
if(item == Item.Flashlight)
{
AudioSource.PlayClipAtPoint(flashlightSound, transform.position, 1);
HUD.HasFlashlight = true;
}
else
HUD.BatteryCount++;
AudioSource.PlayClipAtPoint(batterySound, transform.position, 1);
Destroy(gameObject);
Answer by Adamcbrz · Aug 18, 2013 at 01:25 PM
You need an AudioSource. You will need a reference to an AudioSource that has the AudioClip you want to play assigned to it. Then you can call play of the audio source.
http://docs.unity3d.com/Documentation/Components/class-AudioSource.html http://docs.unity3d.com/Documentation/ScriptReference/AudioSource.html http://docs.unity3d.com/Documentation/ScriptReference/AudioClip.html
Your answer
Follow this Question
Related Questions
Detect if out of trigger 2 Answers
animation and sound on collide 1 Answer
Play audio only once in trigger collision entry? 1 Answer
How to make audio play when passes a collider 2 Answers
How an Enemy could activate a Sound? 3 Answers