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 /
This question was closed Aug 26, 2016 at 06:19 AM by NinjaClicks for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by NinjaClicks · Aug 25, 2016 at 05:53 PM · audioaudiosourceaudio.play

Audio not playing in game

Hi fellow Devs,

Since I am new to Unity and game scripting this might be a simple fix (hoping it will be). I've spent the last 2 days trying different suggested solutions and I come up short somehow.

So here we go. I have a sound that I want to play when I press a button in this case the "a" button, but I can't get the sound to play for the life of me. I have attached an audiosource to the object (letterbox A). With my understanding I should then be able to use the Play function to play the audio.

So what I have done is the following:

  function Update ()
      {
         if (Input.GetKeyDown("a")) 
            {
             var audio : AudioSource = GetComponent.<AudioSource>();
                 audio.Play();
                 Destroy(gameObject);
             }
         }

So what currently happens is, the script compiles successfully and I can run the game in the game window. Once I press the A button the object is removed from the game, but there are no sounds triggered. However if I select the "Play on Awake" setting in the AudioSource that is attached to the object and I start the game, the sound plays.

So I then moved on to make some adjustments by adding the following to the top of the code as I have seen in the unity manual:

 #pragma strict
 @RequireComponent(AudioSource)

However when I save the script I get the following error: BCE0153: 'UnityEngine.RequireComponent' can be applied on one of these targets only : Class. I tried to find it on Google, but my GoogleFu is obviously lackluster and I can't seem to find the problem or if these pieces of code is cardinal to the audioSource.Play.

So I commented that out again. The last thing that I tried to do tonight is as follows in the code:

 var audio : AudioSource;
 
 function Start()
 {
   audio = GetComponent.<AudioSource>();
 }
 
 
 function Update () {
     if (Input.GetKeyDown("a")) 
        {
             audio.Play();
             Destroy(gameObject);
         }
         
        }

When I save the script with those changes I encounter the following error: BCE0004: Ambiguous reference 'audio': PickupA.audio, UnityEngine.Component.audio.

So I am at a wits end. If anyone of you out there can lend me a helping hand I would appreciate it immensely!

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

  • Sort: 
avatar image
1
Best Answer

Answer by v01pe_ · Aug 25, 2016 at 08:19 PM

I'd say remove the line Destroy(gameObject); and you're fine.

What you do is as soon as "A" is pressed you delete the object this script is on with all its components, including the AudioSource that can't continue playing.

If your intention was to remove the script that is starting the audio you would Destroy(this) or set enabled = false.

Also don't call the member variable for the AudioSource audio because every component has this variable (historical reasons), but it's deprecated. Your first approach is also fine. As you don't do a lot with the AudioSource, you don't need to store it, you can GetComponent it directly.

I've never done this in javascript, but the @RequireComponent(AudioSource) should go right above the class declaration, but this only assures, that there has to be an AudioSource on the same object.

Comment
Add comment · Show 2 · 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 NinjaClicks · Aug 25, 2016 at 09:01 PM 0
Share

Hi V01pe_ Thank you so much! Removing the Destroy(gameObject) works like a charm for the sound effect, I didn't think that destroy would trigger before the sound does >.<.

I already have another idea on how to "remove" the object from play. I will also change the variable name to stay with current good practice, thank you for the heads up on that as well.

avatar image v01pe_ NinjaClicks · Aug 26, 2016 at 08:04 AM 0
Share

No problem! Technically the sound will start playing before the object is destroyed, but the playing sound is bound to the AudioSource and as soon as that is destroyed, also the sound stops playing. That means that it will also stop if you destroy the object e.g. 1 second later.

Follow this Question

Answers Answers and Comments

76 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

Related Questions

Audio doesn't sound 0 Answers

audio is not playing during IEnumerator 1 Answer

How to reduce delay when playing sound 0 Answers

Playing multiple sounds at the same time? 1 Answer

Pick Up Sound 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