Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[SOLVED] Seriously cannot add audio or get it to work.

Discussion in '2D' started by Mikal002, Sep 23, 2016.

  1. Mikal002

    Mikal002

    Joined:
    Aug 23, 2015
    Posts:
    10
    For 2 days I've been trying to add some simple audio to my game, a simple explosion sound upon collision, but it just won't work, I've tried so many different things, different scripts from google, NOTHING work and it's really starting to get irritating, I'm beginning to think that Unity is just broken...

    I've created a script called CollisionAudio.cs and attached it to my player, I've also added an AudioSource with the explosion sound to the player aswell, here's the script:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CollisionAudio : MonoBehaviour {
    5.  
    6.     new AudioSource audio;
    7.     public AudioClip explosion;
    8.  
    9.     void Start()
    10.     {
    11.         audio = GetComponent<AudioSource>();
    12.     }
    13.     void OnCollisionEnter2D (Collision2D newCollision) {
    14.  
    15.         if (newCollision.gameObject.tag == "AsteroidCollision")
    16.         {
    17.  
    18.             audio.PlayOneShot(explosion);
    19.         }
    20.     }
    21. }
    I have no idea why it just doesn't work, nothing I've tried does, really appreciate any help, thanks!

    Edit: Before anyone says, the collision objects in question are all tagged correctly, other methods I've tried without involving the collision don't work either (linking it to player death for example), and yes the audio file is set to 2d.
     
  2. Mikal002

    Mikal002

    Joined:
    Aug 23, 2015
    Posts:
    10
    Solved: AudioListener was missing from my main camera...:confused: