Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Destroy and play sound on collision

Discussion in '2D' started by SamVorst, Dec 5, 2019.

  1. SamVorst

    SamVorst

    Joined:
    Nov 28, 2019
    Posts:
    63
    Hi there,

    I'm making a game where you have to hit game objects, the problem is that no code is working for me...
    I just want to destroy and play sound, does somebody know how do this?
    This is my code for destroying a game object and for the audio I don't know how to do that:


    void DestroyGameObject()
    {
    Destroy(gameObject);
    }
     
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    Hi @SamVorst

    I saw you posting similar questions earlier... the things you have asked are (mostly) already explained in introductory tutorial videos and in manual or in API reference.

    "the problem is that no code is working for me..."

    You show barely anything and practically ask others to fill in the part you haven't even started yet. I see no audio related code, so did you google for this? :)

    If you had googled for this issue, you would know, that you will get solutions from Unity Answers, YouTube videos and from manual / API page links.

    Just put Unity + your question to google and you'll see.

    It is not that I don't want to help, but you should learn to help yourself if you want to get something done with code. There is no way around needing to search for all kinds of solutions, you'll have to do that all the time.

    You already know the steps, just google each step and combine those. Then see if you still can't get it done.
     
    Deleted User likes this.
  3. SamVorst

    SamVorst

    Joined:
    Nov 28, 2019
    Posts:
    63
    I'l try!
     
  4. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @SamVorst

    Things you'll need:
    - Collision event methods (either for 2d or 3D, depending on which physics system you use).
    - Audio source
    - Audio clip for that source to play
    - Play audio clip from source when object gets destroyed
    - Note - you'll have to create a separate GameObject for the audio source if you want to destroy your GameObject before the sound ends.
     
  5. SamVorst

    SamVorst

    Joined:
    Nov 28, 2019
    Posts:
    63
    thanks!
     
  6. motivision

    motivision

    Joined:
    Dec 3, 2019
    Posts:
    28
    hi to all

    since all gameobjects destroying automatically before loading new scene
    for me better work SetActive (true) and SetActive (false)

    Code (CSharp):
    1. public GameObject yourscenegameobject;
    2.  
    3.  
    4. void Update() {
    5.      
    6.         //for better compatibility with others platforms
    7.         #if UNITY_ANDROID
    8.         if ()
    9.  
    10.         {  
    11.             yourscenegameobject.SetActive (true);
    12.         }
    13.  
    14.         #endif
    15.  
    16.         else
    17.         {
    18.             yourscenegameobject.SetActive (false);          
    19.         }
    20.  
    21.     }

    sure if you you have a big game project with a lot of gameobjects on the scene where that`s really needed

    try doing like this


    Code (CSharp):
    1.  
    2.  
    3.     void destroygameobjectonyourscene()
    4.     {
    5.         gameObject.tag = "tag your gameobject";
    6.  
    7.         //destroying gameobject after 5 second
    8.         Destroy(gameObject, 5);
    9.     }
    10.  
    11.  
    12.  
    13.  
    14.  
    15.      void OnCollisionEnter2D(Collision2D other)
    16.     {
    17.  
    18.         #if UNITY_ANDROID
    19.         if (other.gameObject.tag == "tag your gameobject")
    20.         {      
    21.             destroygameobjectonyourscene();
    22.         }
    23.  
    24.     else   {
    25.     }
    26.  
    27. }
    28.  
    29.  
    If you have some problems with destroying object on the scene and always see messages on console
    can`t destroy gameobject because this gameobject allready destroying or something else like this,
    just enable or disable your gameobjects on the scene through SetActive (true) and SetActive (false)
     
  7. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    Well if you want to destroy your object, then do so after the sound has played. You can delay the Destroy() like this:

    Code (CSharp):
    1. audioSource.PlayOneShot("YourClipHere"); // Assuming your clip is 0.5 seconds long
    2. Destroy(gameObject, 0.6f); // Delay destroy 0.6th of a second
    Now you can access your game objects sprite renderer and collider and set its activeness to false so it looks destroyed to the player.

    Code (CSharp):
    1. audioSource.PlayOneShot("YourClipHere"); // Assuming your clip is 0.5 seconds long
    2.  
    3. gameObject.GetComponent<SpriteRenderer>().enabled = false;
    4. gameObject.GetComponent<Collider2D>().enabled = false;
    5.  
    6. Destroy(gameObject, 0.6f); // Delay destroy 0.6th of a second
    One last method worth mentioning is:

    Code (CSharp):
    1. PlayClipAtPoint('YourClipHere", gameObject.transform.position, 1.0f);
    2.  
    3. // Enter your audioclip, Vector3 coordinates,and volume
    4. // This creates an audioSource at that location, plays the clip,
    5. // and you can custom set the volume. Pretty cool, right? :)
    The Audio Source is destroyed when the clip finishes.
     
    Last edited: Dec 5, 2019
  8. SamVorst

    SamVorst

    Joined:
    Nov 28, 2019
    Posts:
    63
    Thanks! But with no succes because i got the error cannot convert from 'string' to 'UnityEngine.AudioClip'.

    I had a earlier project where I used this code but with no succes too, does someone know what i'm doing wrong?

    Code (CSharp):
    1.     #region Unity Editor Fields
    2.     [SerializeField] private int _coinValue = 1;
    3.     [SerializeField] private AudioSource _as;
    4.     [SerializeField] private AudioClip _coinSound;
    5.     #endregion
    6.     #region Fields
    7.  
    8.     #endregion
    9.     void Awake()
    10.     {
    11.      
    12.     }
    13.  
    14.     // Start is called before the first frame update
    15.     void Start()
    16.     {
    17.      
    18.     }
    19.  
    20.     // Update is called once per frame
    21.     void Update()
    22.     {
    23.      
    24.     }
    25.  
    26.     private void OnTriggerEnter (Collider target)
    27.     {
    28.         if (target.gameObject.tag == "player")
    29.         {
    30.             //inactive coin
    31.             CoinOnTrigger();
    32.         }
    33.         print("I collide");
    34.     }
    35.  
    36.     private void CoinOnTrigger ()
    37.     {
    38.         //Play the sound on trigger only
    39.         _as.PlayOneShot(_coinSound);
    40.         gameObject.SetActive(false);
    41.     }
    42. }
    43.  
    Thanks Sam
     
  9. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    Is this the exact error message? If not, please copy the entire error in your console or screen shot it.
     
  10. SamVorst

    SamVorst

    Joined:
    Nov 28, 2019
    Posts:
    63
    yup, this is it:
    Assets\Scripts\RedEnemy.cs(11,33): error CS1503: Argument 1: cannot convert from 'string' to 'UnityEngine.AudioClip'
     
  11. SamVorst

    SamVorst

    Joined:
    Nov 28, 2019
    Posts:
    63
    this is my code.

    Code (CSharp):
    1.  using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class RedEnemy : MonoBehaviour
    5. {
    6.     void Start()
    7.     {
    8.         AudioSource.PlayOneShot("explosion"); // Assuming your clip is 0.5 seconds long
    9.  
    10.         gameObject.GetComponent<SpriteRenderer>().enabled = false;
    11.         gameObject.GetComponent<Collider2D>().enabled = false;
    12.  
    13.         Destroy(gameObject, 0.6f); // Delay destroy 0.6th of a second
    14.  
    15.     }
    16.  
    17.     void Update ()
    18.     {
    19.  
    20.     }
    21. }
     
  12. SamVorst

    SamVorst

    Joined:
    Nov 28, 2019
    Posts:
    63
    I finally got it!!!!
     
    MisterSkitz likes this.
  13. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    Is the script provided above your RedEnemy Script? If so, then what is on line 11? That's where you're getting your error from but I don't see anything there.
     
  14. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    I didn't see you posted this until after I just posted. My fault :)
    Care to share your solution in case it may help another?
     
  15. SamVorst

    SamVorst

    Joined:
    Nov 28, 2019
    Posts:
    63
    I found it! but see nothing too.

    Thanks, you helped me a lot!
     
    MisterSkitz likes this.
  16. SamVorst

    SamVorst

    Joined:
    Nov 28, 2019
    Posts:
    63
    Code (CSharp):
    1.  using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class RedEnemy : MonoBehaviour
    5. {
    6.  
    7.     [SerializeField] private AudioSource _as;
    8.     [SerializeField] private AudioClip _explosionSound;
    9.  
    10.     void Start()
    11.     {
    12.  
    13.     }
    14.  
    15.     void Update()
    16.     {
    17.  
    18.     }
    19.  
    20.     private void OnCollisionEnter2D(Collision2D collision)
    21.     {
    22.         if (collision.gameObject.tag == "player")
    23.         {
    24.  
    25.             Destroy(gameObject);
    26.  
    27.             _as.PlayOneShot(_explosionSound);
    28.         }
    29.     }
    30. }
     
  17. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    Was it because you were using OnTriggerEnter() instead of OnCollisionEnter()?
     
  18. SamVorst

    SamVorst

    Joined:
    Nov 28, 2019
    Posts:
    63
    I think so.
     
    MisterSkitz likes this.
  19. thesdfable

    thesdfable

    Joined:
    Jul 6, 2022
    Posts:
    1
    Please do not post answers like these cus I am also looking for a solution for a similar problem and it is annoying to see such answers since they are not helpful but more like scolding. Either write an actual helpful answer or dont write anything. I dont think you can teach a life lesson to a stranger on a unity forum.
     
    CheopisIV36 and steambucky like this.