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 /
avatar image
0
Question by crisro996 · Dec 20, 2015 at 12:43 AM · c#2daudioaudiosource

I have a problem playing an AudioSource

I'm trying to make a bullet play a sound when it hits a wall. The bullet currently has 2 AudioSources - one which plays immediately after a bullet is spawned (this one works) and another one called "Impact" which is supposed to play when I hit a wall. The problem is that I don't know how to properly write the script to make it play that sound.

This is the bits of code that I have:

 public class BulletController : MonoBehaviour
     {
         //some more variables
         private AudioSource ImpactAudio;
 
         // Use this for initialization
         [UsedImplicitly]
         private void Start()
         {
             //some extra things for the player
 
             //I debugged this and it does assign the correct AudioSource
             //to ImpactAudio
             ImpactAudio = GetComponents<AudioSource>()[1];
         }
 
         [UsedImplicitly]
         private void OnTriggerEnter2D(Collider2D otherCollider)
         {
             switch (otherCollider.name)
             {
             //some other cases
 
                 case "Block":
                     PlaySound();
                     //This part works - the bullets are actually destroyed by this
                     Destroy(gameObject);
                     BitBoy.ReloadBullet();
                     break;
 
                 default:
                     Debug.Log(otherCollider.name);
                     break;
             }
         }
 
          //This should(?) play the Impact sound
         private void PlaySound()
         {
             //This does write Impact in the console
             Debug.Log("Impact");
             //Nothing happens.
             ImpactAudio.Play();
         }
     }

I am pretty new to Unity, just started working, but after searching through some previous questions related to this I still don't have a solution.

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
0

Answer by OctoMan · Dec 20, 2015 at 01:30 AM

 ImpactAudio = GetComponents<AudioSource>()[1];

shouldn't it be ?

 ImpactAudio = GetComponents<AudioSource>();

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 Statement · Dec 20, 2015 at 01:36 AM 0
Share

No, the former is picking the second audio source on the game object, the latter is picking ALL of the audio sources on the game object.

avatar image OctoMan Statement · Dec 20, 2015 at 01:40 AM 0
Share

Why would you use more than 1 audio source?

Anyways, did you added a audio clip into your audio source?

avatar image
0

Answer by Statement · Dec 20, 2015 at 01:40 AM

 PlaySound();
 Destroy(gameObject);

It appear that the problem is that you destroy the game object that is meant to play the sound. You can't destroy the object and expect the same object to do stuff after it has been destroyed.

To solve your problem, tell another game object to play the sound at its location.

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 OctoMan · Dec 20, 2015 at 01:43 AM 0
Share

Sounds right, or add some delete time:

 Destroy(gameObject, 1f);

avatar image Statement OctoMan · Dec 20, 2015 at 02:35 AM 0
Share

But that doesn't make much sense unless you also remove colliders/graphics/other scripts from the game object. It'd be silly to have an object linger around only so the audio source can continue playing.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

C# Setting AudioMixerGroup through code 2 Answers

Audio help 0 Answers

Second AudioClip won't play 0 Answers

How do I get the parent from a AudioMixerGroup Object 2 Answers

Second AudioClip won't play 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