Search Unity

Playing Sound when it Collides with more than One Object

Discussion in 'Audio & Video' started by djzski, Oct 27, 2015.

  1. djzski

    djzski

    Joined:
    Aug 31, 2015
    Posts:
    6
    Sorry if this has already been posted about. I'm not very good with the audio side of things, and I'm trying to get it so that, when an object hits a different the walls, the same sound is made. However, all the tutorials on the subject that I have come across so far, have only helped in playing the sound once, and then never again afterward.

    This is my most recent attempt:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [RequireComponent(typeof(AudioSource))]
    5. public class SoundOnCollision : MonoBehaviour {
    6.  
    7.     public AudioSource audio;
    8.  
    9.     void Start() {
    10.          audio = GetComponent<AudioSource>();
    11.  
    12.  
    13.     }
    14.  
    15.     void OnCollisionEnter (Collider other)
    16.     {
    17.         if (other.gameObject.tag == "Wall 2") {
    18.             audio.Play();
    19.         }
    20.    
    21.     }
    22. }
    Any chance I could get some advice? Thanks in advance!
     
  2. BrandyStarbrite

    BrandyStarbrite

    Joined:
    Aug 4, 2013
    Posts:
    2,076
    Triggers might be a better idea. :p