# ERROR: MissingComponentException

I want my gameObject to play an audio clip when it collides with other gameObjects. So I attached a script named "ImpactSound" with it, but it's keep showing MissingComponentException: There is no "AudioSource" attatched to the ball(clone)game object, but a script is trying to accept it.

    using UnityEngine;
using System.Collections;

public class ImpactSound : MonoBehaviour {

public AudioClip impact;
AudioSource source;

// Use this for initialization
void Start () {
source = GetComponent<AudioSource>();
}

// Update is called once per frame
void OnCollisionEnter2D() {
Debug.Log ("Sound was supposed to play on collision!");
source.PlayOneShot (impact);
}
}

• Have you attached an AudioSource component to the game object? – Tyyppi_77 Nov 23 '16 at 5:56
• Yes I've attached an audio clip on impact..@Tyyppi_77 – Jatin Nov 23 '16 at 6:07
• That's not what I asked. Is there a AudioSource component in the object? – Tyyppi_77 Nov 23 '16 at 6:23

It seems you don't have an Audio Source attached with that following game object.

• Well, that worked. But now I want to make it sound only when it collides with a specific gameObject. How am I supposed to do so? – Jatin Nov 23 '16 at 9:01
• The PlayOnAwake is surely checked. Uncheck it. – Hellium Nov 23 '16 at 10:10
• @Jatin, use tag to that game object. Its quite easy. – Shuvro Sarkar Nov 23 '16 at 10:55

The RequireComponent attribute automatically adds required components as dependencies.

When you add a script which uses RequireComponent to a GameObject, the required component will automatically be added to the GameObject. This is useful to avoid setup errors. For example a script might require that a AudioSource is always added to the same GameObject. Using RequireComponent this will be done automatically, thus you can never get the setup wrong. Note that RequireComponent only checks for missing dependencies during the moment the component is added to a GameObject. Existing instances of the component whose GameObject lacks the new dependencies will not have those dependencies automatically added.

using UnityEngine;
using System.Collections;

[RequireComponent (typeof (AudioSource))]
public class ImpactSound : MonoBehaviour
{
public AudioClip impact;
AudioSource source;

// Use this for initialization
void Start()
{
source = GetComponent<AudioSource>();
source.clip = impact;
}

// when collide with other with tag enemy play sound
void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.tag == "Enemy")
{
source.PlayOneShot(impact);
}
}
}


other way for prevent missing error that checking object have AudioSource component.in this example I use Property for checking it.

using UnityEngine;
using System.Collections;

public class ImpactSound : MonoBehaviour
{
public AudioClip impact;
AudioSource source
{
get
{
if (GetComponent<AudioSource>() == null)
{
return GetComponent<AudioSource>();
}else
{
return GetComponent<AudioSource>();
}
}
set
{
source.clip = impact;
}
}

void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.tag == "Enemy")
{
source.PlayOneShot(impact);
}
}
}


if you don't like have error you can use AudioSource.PlayClipAtPoint , in this way your object Instantiate empty gameObject with AudioSource component in position that is easy way for play sound.

using UnityEngine;
using System.Collections;

public class ImpactSound : MonoBehaviour
{
public AudioClip impact;

// when collide with something play sound
void OnCollisionEnter2D()
{
Debug.Log("Sound was supposed to play on collision!");
AudioSource.PlayClipAtPoint(impact, transform.position);
}
}