-1
\$\begingroup\$

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);
    }
    }
\$\endgroup\$
  • 1
    \$\begingroup\$ Have you attached an AudioSource component to the game object? \$\endgroup\$ – Tyyppi_77 Nov 23 '16 at 5:56
  • \$\begingroup\$ Yes I've attached an audio clip on impact..@Tyyppi_77 \$\endgroup\$ – Jatin Nov 23 '16 at 6:07
  • 1
    \$\begingroup\$ That's not what I asked. Is there a AudioSource component in the object? \$\endgroup\$ – Tyyppi_77 Nov 23 '16 at 6:23
2
\$\begingroup\$

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

\$\endgroup\$
  • \$\begingroup\$ 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? \$\endgroup\$ – Jatin Nov 23 '16 at 9:01
  • \$\begingroup\$ The PlayOnAwake is surely checked. Uncheck it. \$\endgroup\$ – Hellium Nov 23 '16 at 10:10
  • \$\begingroup\$ @Jatin, use tag to that game object. Its quite easy. \$\endgroup\$ – Shuvro Sarkar Nov 23 '16 at 10:55
0
\$\begingroup\$

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)
            {
                gameObject.AddComponent<AudioSource>();
                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);
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.