Search Unity

Audio Help with collision sound needed, thanks

Discussion in 'Audio & Video' started by MikeUnity123, Oct 1, 2018.

  1. MikeUnity123

    MikeUnity123

    Joined:
    Aug 8, 2018
    Posts:
    4
    Hi,

    I'm new to games development, Unity and C#, so please forgive me.

    I am creating my first game (a simple 2D platform) and am having trouble with trying to get a collision sound working.

    I have a player object that runs and jumps across platforms (all works great). However if I miss the platform the player falls into water, and I want to add a splash sound to it.

    I have added the following script and nothing happens.

    Code (CSharp):
    1. public class SplashSound : MonoBehaviour {
    2.  
    3.     public AudioSource SplashSource;
    4.     public AudioClip Splashed;
    5.  
    6.  
    7.     // Use this for initialization
    8.     void Start () {
    9.  
    10.         SplashSource = GetComponent<AudioSource> ();
    11.     }
    12.  
    13.  
    14.     void OnCollisionEnter(Collision collision) {
    15.         if (collision.gameObject.tag == "Player") {
    16.          
    17.             SplashSource.PlayOneShot(Splashed);
    18.             Debug.Log("Played");
    19.  
    20.         }
    21.        
    22.     }
    23.  
    24. }

    I have tried adding it to both the Player and the WaterTile (the WaterTile has a box collider), as separate triggers separately but neither makes a sound. (the example code above is shown as the Player code. I get no error messages or compiler errors).

    I have tried ticking the 'on wake' and that does make the sound, so I know I can hear it no problem. I just don't seem to be able to get it to work on collision.

    The Player has an Audio Source component with an AudioClip called Splash, and the a component for the script as above.

    What am I doing wrong?

    Your help is much appreciated.
     
  2. MikeUnity123

    MikeUnity123

    Joined:
    Aug 8, 2018
    Posts:
    4
    Wow, no one can help? I assume this to be a relatively common issue? but despite hours of looking and trying different scripts can't seem to get it to work and hoping someone might see a simple mistake I may have made?
     
  3. r618

    r618

    Joined:
    Jan 19, 2009
    Posts:
    1,305
    It's a physics, not audio issue, have a look here: https://docs.unity3d.com/Manual/CollidersOverview.html
     
  4. MikeUnity123

    MikeUnity123

    Joined:
    Aug 8, 2018
    Posts:
    4
    OK, thanks. I'll take a look at that.

    Cheers
    Mike
     
  5. MikeUnity123

    MikeUnity123

    Joined:
    Aug 8, 2018
    Posts:
    4
    For future reference to anyone looking at this. The answer was indeed a script error. The error was due to using 'void OnCollisionEnter(Collision collision)'. Instead of void OnCollisionEnter2D(Collision2D collision).
     
  6. Deleted User

    Deleted User

    Guest

    Thanks a lot ... almost three years later