Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 AlfredB · Jul 27, 2012 at 08:14 PM · triggersound

I want my trigger to play sound only once!

I just wanted to start of saying that im not so good at scripting. I have this script that makes it so when gameobject, tag "Player" enters the trigger area, sound plays. Well when i enter the area it plays the sound but when i enter the area again the sound plays again and i want it to only be able to play once. Here is the script

var Sound : AudioClip;

function OnTriggerEnter(){

audio.PlayOneShot(Sound);

}

(javascript)

Comment
Add comment · Show 1
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 rayith · Feb 20 at 04:56 AM 0
Share
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class soundtrigger : MonoBehaviour
 {
     public AudioClip SoundToPlay;
     public float Volume;
     AudioSource audio;
     public bool alreadyPlayed = false;
 
     void Start()
     {
         audio = GetComponent<AudioSource>();
     }
     
     void OnTriggerEnter()
     {
         if (!alreadyPlayed)
         {
            audio.PlayOneShot(SoundToPlay, Volume);
            alreadyPlayed = true;
         }
     }
 }

2 Replies

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

Answer by FLASHDENMARK · Jul 27, 2012 at 08:20 PM

 var Sound : AudioClip;
 
 private var hasPlayed = false;
 
 function OnTriggerEnter(){
     if(!hasPlayed){
         audio.PlayOneShot(Sound);
         hasPlayed = true;
     }
 }

Here you go.

Comment
Add comment · Show 3 · 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 AlfredB · Jul 27, 2012 at 08:44 PM 0
Share

Thank you very much!

avatar image AlgorithMan · Feb 19, 2014 at 12:31 PM 0
Share

I like the idea to destroy the triggering object - but why would you still need to test and set the hasPlayed variable? If the trigger is gone, then the sound should not be triggered again anyways?

avatar image KnightRiderGuy · Dec 01, 2014 at 06:30 PM 0
Share

This works really well for an on$$anonymous$$ouseEnter on a texture GUI buttons too. Thanks for posting this code OrangeLightning :)

avatar image
6

Answer by IndieScapeGames · Jul 27, 2012 at 08:22 PM

Taking OrangeLightening's example and going a bit further, you could also delete the object you are colliding with.

var Sound : AudioClip;

private var hasPlayed = false;

 function OnTriggerEnter(){
     if(!hasPlayed){
         audio.PlayOneShot(Sound);
         hasPlayed = true;
     Destroy(GameObject); //untested
     }
 }

Also, make sure that you have attached an Audio Source to the object, and that your main camera has 1 (one) audio listener.

Comment
Add comment · Show 5 · 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 FLASHDENMARK · Jul 27, 2012 at 08:26 PM 0
Share

You could do that as well. +1

avatar image IndieScapeGames · Jul 27, 2012 at 09:58 PM 0
Share

Thanks for the +1, I really want to get my $$anonymous$$arma up (is it $$anonymous$$arma here like on StackOverflow??) and help Unity newcomers.

avatar image FLASHDENMARK · Jul 27, 2012 at 10:02 PM 0
Share

The FAQ describes it as $$anonymous$$arma/Reputation.

http://answers.unity3d.com/page/faq.html

avatar image Seth-Bergman · Jul 27, 2012 at 10:08 PM 3
Share

that would be problematic though, as it stands...

Destroy(gameObject); // lower case

but if you destroy the object, I think the sound may be cut short

you could add

if(!audio.isPlaying())

Destroy(gameObject);

to let the sound play through

avatar image fafase · Jul 29, 2012 at 05:47 PM 0
Share

Continuing with the idea of the cutting sound:

 var Sound : AudioClip;

 private var hasPlayed = false;

 function OnTriggerEnter(){
     if(!hasPlayed){
         AudioSource.PlayOneShot(Sound);
         hasPlayed = true;
     Destroy(gameObject); 
     }
 }

Then an audio source object is created independant of the gale object and should be destroyed when the clip is over after the object is already gone.

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

14 People are following this question.

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

Related Questions

Triggering random sound on player 1 Answer

Totally new noob to mecanim and unity itself - Trigger animation? 0 Answers

On trigger , sound play 2 Answers

Activate sound without Pro filters 0 Answers

How can i start a sound when a specific object is players frustrum? 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