How to load an audiosource on trigger once?
Hey, I'm a sophomore in high school learning Unity and probably the hardest part is learning how to work with triggers in coding. So I'm using Unity's Fps Controller and have some audio clips of me saying stuff that I want to activate when the player, tagged "Player" (if that matters), walks through a trigger, it would start the audio clip. I don't know how to do this. I've tried doing void OnTriggerEnter(Collider co) { { GetComponent().Play(); } } //the script is C#
but that didn't work. Well it did, but I can trigger it multiple times which I DON'T want. I want it so that once you touch it, it plays the audioclip only ONE time. If you would know how to do this that would be great. Thanks and I'll be clear as possible.
Answer by AlejandroBoss · Feb 16, 2017 at 08:17 PM
I figured it out. this is the script that I used with Unity 5.5. This script is with JavaScript and it works great with Unity's FPS Controller. What you do have to remember when working with this script is that you attach it to the object that you want to play the sound and set it to trigger. There is this space in the inspector where you can insert your audio, but that will not work. You must add an audiosource that doesn't play on awake (or it does, it's up to you) and that's pretty much it.
var Sound : AudioClip;
private var hasPlayed = false;
function OnTriggerEnter(){ if(!hasPlayed){ audio.PlayOneShot(Sound); hasPlayed = true; } } { GetComponent().Play(); } } }
You attach the script to the object that is set as a trigger and attach an audiosource to the object. In the audiosource you put the audio clip that you want it to play when it's triggered. That script is tested and works.
Answer by Scoutas · Feb 03, 2017 at 10:05 PM
Is that how you've written the code? play audioclip. "MeTalking"
?
That's incorrect in syntax. Okay, so, I will make a few assumptions. You have this script, a collider and an audiosource on this same object. You have to get the audiosource component, and you can do that by saying AudioSource source = GetComponent<AudioSource>();
. Once you have it, you do source.Play()
and it should play the sound that you have assigned in the audiosource component.
If anything's still confusing, ask away.
Yes, I do have the script, a collider, and audio source on the same object. Now I have a question about your line of code. Does, AudioSource source = GetComponent(); get the audioclip that I have on the gameobject? Also would this be the script?
//this finds the game object called "Player" in the scene
if (co.name == "Player") // If what collided with the trigger is called "Player"
AudioSource source = GetComponent<AudioSource>():
source.Play()
}
If that's not correct, please help me fix it. I feel like I'm on the right track, but I feel like its wrong. I would appreciate it if you would help me @Scoutas with this, as it would really impress my $$anonymous$$cher and me. Thank you in advance P.S: does this loop the audioclip because I don't want that to happen.
Let me illustrate it:
public class PlayAudioOnTrigger : $$anonymous$$onoBehaviour {
void OnTriggerEnter(Collider co)
{
//this finds the game object called "Player" in the scene
if (co.name == "Player"){
AudioSource source = GetComponent<AudioSource>();
}
source.Play();
}
}
This will not work, because you declare the source inside of the if statement, and it doesn't exist for anyone that's outside of the if statement. To fix it, you simply need to move source.Play();
into the if statement.
public class PlayAudioOnTrigger : $$anonymous$$onoBehaviour {
void OnTriggerEnter(Collider co)
{
//this finds the game object called "Player" in the scene
if (co.name == "Player"){
AudioSource source = GetComponent<AudioSource>();
source.Play();
}
}
}
This will work, because you declare source
inside of the if statement, and you call it inside of the if statement. Basically, because source
exists inside the if statement, you can call methods on it inside of the if statement.
This line AudioSource source = GetComponent<AudioSource>();
gets the component of type 'AudioSource' that is attached to the object. If your 'AudioSource' component, has an AudioClip set inside the inspector then when you call source.Play()
it will play that AudioClip.
As for looping, you can set it in the inspector, on the AudioSource component, or in code, by saying source.loop = false
.
I hope it's clear enough for you to understand, but if anything - leave a comment, I'll try to answer.
Thanks, I shall try it in a while and notify you if it works or doesn't. Thanks a lot really.
I tried it, but I got some errors. This is the script that I used but it does not work.
using UnityEngine; using System.Collections;
public class PlayAudioOnTrigger : $$anonymous$$onoBehaviour {
void OnTriggerEnter(Collider co)
{
//this finds the game object called "Player" in the scene
if (co.name == "Player") // If what collided with the trigger is called "Player"
AudioSource source = GetComponent<AudioSource>():
source.Play();
}
}
I get an error on the part that you gave me. It says "Embedded statement cannot be a declaration or labeled statement. And on source it says "The name source does not exist in the current context. If your could help me by either giving me the script or editing $$anonymous$$e that would be great @ Scoutas. Thanks
Put in some curly braces around your if
method.
public class PlayAudioOnTrigger : $$anonymous$$onoBehaviour {
void OnTriggerEnter(Collider co)
{
//this finds the game object called "Player" in the scene
if (co.name == "Player") { <---- HERE
AudioSource source = GetComponent<AudioSource>():
source.Play();
} <---- HERE
}
}
If it's not wrapped in curly braces, declaring a variable will throw an error. That's why you get the "Embedded..." error.
As for the next error, technically, source.Play();
is actually outside of the 'if' statement, and because of that, it can't see the source
variable, because it's "deeper down" so to speak.
Your answer
![](https://koobas.hobune.stream/wayback/20220612110249im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
HELP WITH TOUCH SCRIPT 0 Answers
maybe audiosource problem 1 Answer
Trying to make a Parkour Platformer! 0 Answers
TRIGGER TO LAUNCH GAME 2 Answers