Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by AlejandroBoss · Feb 03, 2017 at 09:31 PM · triggerscripting beginneraudioclipplayoneshot

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AlejandroBoss · Feb 07, 2017 at 06:21 PM 0
Share

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.

avatar image Scoutas AlejandroBoss · Feb 07, 2017 at 08:51 PM 0
Share

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.

avatar image Scoutas · Feb 07, 2017 at 06:44 PM 0
Share

@AlejandroBoss

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.

avatar image AlejandroBoss Scoutas · Feb 07, 2017 at 07:03 PM 0
Share

Thanks, I shall try it in a while and notify you if it works or doesn't. Thanks a lot really.

avatar image AlejandroBoss Scoutas · Feb 07, 2017 at 08:13 PM 0
Share

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

avatar image Scoutas AlejandroBoss · Feb 07, 2017 at 08:18 PM 0
Share

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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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

Enable/Disable MeshRenderer on trigger enter/exit 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges