Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by TheGoliath · May 01, 2012 at 08:18 PM · audiosounddistance

Play a sound only once?

I have a script attached to an empty game object that waits until the player is at a distance of 10 from it, then plays a sound. The only problem is that when I get close to the game object the sound starts repeating and doesn't stop. How can I make it so the sound just plays once and never again? I've been looking around for solutions for hours now, and I've tried everything I can think of, but nothing works right.

 var player : GameObject;
 var VoiceClip : AudioClip;

 function Start() {
     player = GameObject.Find("lindamayfield");

 }
         
 function Update(){


     //If the player is close then play the sound
     if(Vector3.Distance(transform.position, player.transform.position) < 10)
     {
         audio.clip = VoiceClip;
         audio.Play();
     }
    

}

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
1
Best Answer

Answer by Comaleaf · May 01, 2012 at 08:39 PM

You could use a Collider to on trigger when the player enters the collider (using `OnCollisionEnter`).

Alternatively you could keep track of whether the sound has already been triggered like so:

 var player : GameObject;
 var VoiceClip : AudioClip;
 var hasPlayed = false;
 
 function Start() {
     player = GameObject.Find("lindamayfield");
 }
 
 function Update(){
     //If the player is close then play the sound
     if(Vector3.Distance(transform.position, player.transform.position) < 10)
     {
         if (hasPlayed == false)
         {
             hasPlayed = true;
             audio.clip = VoiceClip;
             audio.Play();
         }
     }
     else
     {
         hasPlayed = false;
     }
 }

(This assumes you'd want the sound to be able to play again when the player leaves the area, otherwise just remove the bottom hasPlayed = false;)

Comment
Add comment · Show 1 · 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 TheGoliath · May 01, 2012 at 11:23 PM 0
Share

Thank you! This works perfectly! I spent hours trying to figure this out and all I had to do was just add a few simple commands. Thanks again.

avatar image
0

Answer by LukaKotar · May 01, 2012 at 09:25 PM

var player : GameObject; var VoiceClip : AudioClip;

function Start() { player = GameObject.Find("lindamayfield");

}

function Update(){

 //If the player is close then play the sound
 if(Vector3.Distance(transform.position, player.transform.position) < 10)
 {
     audio.PlayOneShot(VoiceClip);
 }

}

Comment
Add comment · Show 1 · 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 Comaleaf · May 01, 2012 at 09:45 PM 0
Share

PlayOneShot does not make an AudioSource only play once. What it does do is play a particular clip on an audio source without you having to set that clip to be the AudioSource's clip.

If you call PlayOneShot multiple times, it will play multiple times.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Audio mixed, blending on distance ? 1 Answer

FMOD Distance Parameter: At 0 distance the sound is very quiet when it's supposed to be full volume? 0 Answers

Sound volume doesn't change no matter what distance? 0 Answers

3D Sound Max Distance Not Working. 0 Answers

Need help making an audio trigger. 2 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