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 TheSurvival · Feb 25, 2014 at 04:05 PM · triggersound

How to make the trigger work only once. (SOUND)

I know there are many questions on the topic but none fit my interests. So, I'm working on a horror game, and I am a real begginer but with some knowledge. So I have my trigger and this jumpscare script: #pragma strict

var Alien_Creature : GameObject;

function Start () {

Alien_Creature.SetActive(false);

}

function OnTriggerEnter () {

Alien_Creature.SetActive (true);

}

function OnTriggerExit ()

{

Alien_Creature.SetActive(false); Destroy(Alien_Creature); }

Everytime I walk through the trigger, the SOUND starts playing. I am not sure if it's the jumpscare script's fault or the sound script's fault but here is the sound script: function OnTriggerEnter(other: Collider){

if (!audio.isPlaying){

audio.Play(); Destroy.audio(); } } Help Please :)

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Dblfstr · Feb 25, 2014 at 04:21 PM

Use a bool "hasPlayed" to see if the audio has played once.

 var hasPlayed = false;
 
 function OnTriggerEnter(other: Collider){
 
     if (!audio.isPlaying){
     
        if(!hasPlayed){
           audio.Play();
           hasPlayed = true;
        }  
     } 
 }
Comment
Add comment · Show 9 · 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 TheSurvival · Feb 25, 2014 at 04:33 PM 0
Share

I told you I'm kinda of a newbie, where do I need to place this? $$anonymous$$ake a new script, add it at the beggining of the sound script or...?

avatar image Dblfstr · Feb 25, 2014 at 04:37 PM 0
Share

This was just a modification to your soundscript.

avatar image Dblfstr · Feb 25, 2014 at 04:45 PM 0
Share

Also, if the sound script is attached to the trigger that plays the sound. And you only want to play the sound once. You could always disable the trigger once it been used.

this.setActive = false; after you audio has played.

avatar image TheSurvival · Feb 25, 2014 at 04:45 PM 0
Share

It tells me to insert a semi-colon at the end (1,5) I see it already has.

avatar image Dblfstr · Feb 25, 2014 at 04:46 PM 0
Share

I'm not sure if I got the javascript syntax right on declaring a bool variable. I use C#.

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

22 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

I want my trigger sound only to play once! 0 Answers

Play sound on trigger, sound is coming from the trigger 1 Answer

Sound script problem. 0 Answers

Playing sound on collide with a 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