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 18minimus18 · Jan 28, 2020 at 08:08 PM · unity 52d gamecodepage

Cant Play Sound On Destroy

hi trying to play a sound on Destroy but it's not playing I don't know what I'm doing wrong really and not a lot I can explain

here's the code maybe you experts can help me, it could just be me not looking at it the write way I don't know

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Asteroids : MonoBehaviour
 {
     public float Torque;
     public Rigidbody2D rb;
 
     public GameObject asteroidLarge;
     public GameObject asteroidSmall;
 
     void Start()
     {
         float torque = (Torque);
 
         rb.AddTorque(torque);
 
     }
 
     void OnTriggerEnter2D(Collider2D other)
     {
         if (other.CompareTag ("bullet"))
         {
 
             Destroy(other.gameObject);
             {
 
                 if (asteroidLarge);
 
                 Destroy(gameObject);
 
             }
                 
         }
     }
 
 }


and here is the code for the SoundManagerScript

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class SoundManagerScript : MonoBehaviour
 {
 
  public static AudioClip PlayerDeathSound, playerBulletSound, enemyDeathSound, AsteroidExplosion01, AsteroidExplosion02, AsteroidExplosion03;
  static AudioSource audioSrc;
 
 
     void Start()
     {
         PlayerDeathSound       =    Resources.Load<AudioClip>("PlayerDeath");
         playerBulletSound      =    Resources.Load<AudioClip>("Fire");
         enemyDeathSound        =    Resources.Load<AudioClip>("EnemyDeath");
         AsteroidExplosion01    =    Resources.Load<AudioClip>("Asteroid01");
         AsteroidExplosion02    =    Resources.Load<AudioClip>("Asteroid02");
         
 
         audioSrc = GetComponent<AudioSource>();
             
     }
 
    
     void Update()
     {
         
     }
 
     public static void PlaySound (string clip)
     {
         switch (clip)
         {
             case "Asteroid01":
                 audioSrc.PlayOneShot(AsteroidExplosion01);
                 break;
         }
     }
 
 }

I've been working on this for since 6 am and its 8 pm now and I'm getting tired and I've still got to figure out how to play an animation after destroy too, I gave up on that I couldn't figure it out but 1 thing at a time

Regards Andy

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 Hellium · Jan 29, 2020 at 07:49 AM 0
Share

Where do you call Sound$$anonymous$$anagerScript.PlaySound?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by sacredgeometry · Jan 28, 2020 at 11:42 PM

I am assuming you are destroying the gameobject with your sound manager script attached to it.


Long story short you should probably not be destroying anything in Unity. Destroy deallocates the gameobject and flags it for garbage collection.

Garbage collection causes performance dips so avoiding deallocation during gameplay is generally a good idea.

Try spawn pooling, which should fix your problem if your sound isn't directional. if it is you could hide.disable it and wait for the sound to finish before returning it to your spawn pool.

Comment
Add comment · Show 1 · 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 18minimus18 · Jan 29, 2020 at 05:34 PM 0
Share

so I've not done a lot with pooling, and don't really know where to start, the way the game is set up is the asteroids get made invisible when they leave the game area I'm only destroying them when I shot them so there not a lot going on one there destroyed, but I could have my logic all wrong, what would the code look like with disabling a object and playing a sound, not only that I have to play an animation after it's destroyed too but I can't get any of it to work the way I want it too

avatar image
0

Answer by adi7b9 · Jan 29, 2020 at 07:55 AM

 void OnTriggerEnter2D(Collider2D other)
      {
          if (other.CompareTag ("bullet"))
          {
              Destroy(other.gameObject);
              {
                  //play the sound here
                  if (asteroidLarge);//this ";"
                  {
                      //play sound here
                      Destroy(gameObject);
                   }
              }
                  
          }
      }
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 sacredgeometry · Jan 29, 2020 at 10:03 AM 0
Share

That is not valid c#

avatar image Hellium sacredgeometry · Jan 29, 2020 at 10:26 AM 0
Share

Yes it is.

You can have code blocks whenever you want in a C# function. It just does not make much sense and could be simplified, but it's allowed.

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

245 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 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 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 can I spawn different GameObjects using IEnumerator? 1 Answer

How do I know that an object is not coliding without using OnCollisionExit? 1 Answer

Game Object Layer changes automatically on runtime.,Game Object's Layer automatically changes in runtime? 1 Answer

Collect pickup if mouse button is not pressed 0 Answers

Changing array of display images with other set of images on user button press event 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