Search Unity

  1. Get all the Unite Berlin 2018 news on the blog.
    Dismiss Notice
  2. Unity 2018.2 has arrived! Read about it here.
    Dismiss Notice
  3. We're looking for your feedback on the platforms you use and how you use them. Let us know!
    Dismiss Notice
  4. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  6. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  7. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  8. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Problem with audio on collision

Discussion in 'Scripting' started by iMihai9, Jun 14, 2018.

  1. iMihai9

    iMihai9

    Joined:
    Jun 4, 2018
    Posts:
    11
    Hi .. I have that code :
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5. using UnityEngine.UI;
    6. using UnityEngine.Audio;
    7.  
    8. public class HazardCollisionFunction : MonoBehaviour {
    9.  
    10.     #region Variables
    11.  
    12.     //Public
    13.     public AudioSource effects;
    14.  
    15.     //Private
    16.     private UIFunctions uiFunctions;
    17.     public ParticleSystem hazardDustParticles;
    18.  
    19.     #endregion
    20.  
    21.     #region UnityFunctions
    22.     void Start()
    23.     {
    24.         uiFunctions = GameObject.FindGameObjectWithTag("GameManager").GetComponent<UIFunctions>();
    25.         effects = GetComponent<AudioSource>();
    26.     }
    27.  
    28.     void Update()
    29.     {
    30.  
    31.  
    32.     }
    33.     #endregion
    34.  
    35.     private void OnCollisionEnter(Collision collision)
    36.     {
    37.         if(collision.gameObject.tag == "Platform")
    38.         {
    39.             Destroy(Instantiate(hazardDustParticles.gameObject, transform.position, Quaternion.identity), hazardDustParticles.startLifetime);
    40.             effects.Play();
    41.             Die();
    42.            
    43.         }
    44.         if (collision.gameObject.tag == "Player")
    45.         {
    46.  
    47.             effects.Play();
    48.            
    49.             uiFunctions.GameEnded();
    50.            
    51.         }
    52.     }
    53.  
    54.     public void Die()
    55.     {
    56.         Destroy(gameObject);
    57.     }
    58.  
    59. }
    60.  
    Annd when my object hit the platform , the sound don't start , but when hit the player sound start , if i delete die on collision with platform , sound work , but my object doesn t destroy..
    Sorry for my bad english :)
     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    643
    It's because you're destroying the AudioSource when you call Die(). Try this:
    Code (CSharp):
    1.   if(collision.gameObject.tag == "Platform")
    2.         {
    3.             Destroy(Instantiate(hazardDustParticles.gameObject, transform.position, Quaternion.identity), hazardDustParticles.startLifetime);
    4.             effects.transform.SetParent(null);
    5.             effects.Play();
    6.             Die();
    7.             GameObject.Destroy(effects, 5f);
    8.          
    9.         }
    This removes the AudioSource from the game object, so that the audio source doesn't get destroyed when you call Die. Then the AudioSource will be destroyed 5 seconds later.
     
  3. iMihai9

    iMihai9

    Joined:
    Jun 4, 2018
    Posts:
    11
    Doesn't work .. i put that but isn t sound when Game Object hit target
     
  4. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    643
    If you put a breakpoint within the `if` block, is it getting hit?
     
    Doug_B likes this.
  5. iMihai9

    iMihai9

    Joined:
    Jun 4, 2018
    Posts:
    11
    I put the sound collision on platform , and when game object hit platform , sound is activated :)
    Thank's you !
     
  6. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    643
    Fair enough. Just keep it in the back of your mind for later that if you want some sound to play when an object is destroyed, the AudioSource can't remain on the destroyed object, it will stop playing immediately. It's pretty common for objects to play a sound when destroyed, so the approach of un-parenting the audio source before playing it is common and works well.