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 /
avatar image
0
Question by okyesandrei · Jun 26, 2019 at 11:22 PM · soundsound effects

PlaySound Once After Activated

So I have a door that Activates once the Player Enters Its Collider. I only need the sound to play once but I am terrible with Scripting. After the door is already opened the Player can collide with it and it will play the sound again, as i intend it to only play once. Ive Tried Changing _audioSource.Play(); to _audioSource.PlayOneShot(); but it doesnt seem to work. I have also watched numerous tutorials and looked at other peoples questions but they all seem to be extremely specific and dont match with my problem.

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class DoorScript : MonoBehaviour {

 private Animator _animator;
 private AudioSource _audioSource;

 private void Awake()
 {
     _animator = GetComponent<Animator>();
     _audioSource = GetComponent<AudioSource>();
 }

 private void OnTriggerEnter(Collider other)
 {
     if (other.CompareTag("Player"))
     {
         _animator.SetBool("open", true);

         _audioSource.Play();
     }
 }

}

Comment
Add comment · Show 2
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 Loki18000 · Jun 27, 2019 at 01:15 AM 0
Share

What is it not doing? Is the sound not playing at all? Is the animator bool setting? Try debugging.

avatar image okyesandrei Loki18000 · Jun 27, 2019 at 01:28 AM 0
Share

Sorry, i forgot to mention. After the door is opened the player can collide with the door again and the sound plays again.

1 Reply

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

Answer by Andromedog · Jun 27, 2019 at 01:37 AM

So two things:

First, make sure that the audiosource isn't called on multiple times. This could happen for multiple reasons due to multiple colliders on the player, or the sound being called in other scripts.

Otherwise, the audiosource could be on 'loop'. If 'loop' is off the sound should only play once.

Second, is a question. Is the door animated? If so, there may be a easier way of timing your sound and having it only play once.

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 okyesandrei · Jun 27, 2019 at 01:41 AM 0
Share

Yes Its animated.

avatar image Andromedog okyesandrei · Jun 27, 2019 at 01:57 AM 0
Share

What you can do then is use an Animation_Event to call your sound. When you look at your animation in the "Animation Tab", it will be the symbol that kind of looks like a banner. Place that in your animation and have a script that calls for the sound to play. The sound will only play when it reaches that point in the animation.

If you want more details on Animation_Events, check out this video:

https://www.youtube.com/watch?v=mTAjzdR65eo

avatar image okyesandrei · Jun 27, 2019 at 02:03 AM 1
Share

Just got it to work! thanks! I didnt know that you could activate sounds through animations :)

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

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

Related Questions

How to play multiple audio files from one script 1 Answer

Unity plays only a few gameobject's sound 2 Answers

Sound play in spot of particle collision 1 Answer

AudioClip calculates length wrong 1 Answer

Roll A Ball Sound 1 Answer


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