-1
\$\begingroup\$

I'm trying to play a sound on death, but keep getting a null error. I know it is something in my code, and I have already tried looking at an alternate question.

This is my code:

public class BallHealth : MonoBehaviour 
{
    public float maxPositionY = -10.0f;

    void Update () 
    {
        float positionBallY = transform.position.y;

        if (positionBallY <= maxPositionY)
        {
            StartCoroutine (RestartLevel ());
        }
    }

    IEnumerator RestartLevel()
    {
        AudioSource audio = new AudioSource ();

        audio.clip = (AudioClip)Resources.Load("GameOver") as AudioClip;
        audio.Play();
        yield return new WaitForSeconds(audio.clip.length);
        Debug.Log("You Died....");
        SceneManager.LoadScene("LevelOne");
    }
}

I already locate my file, in the resource folder.

Project structure

How do I fix the null error?

\$\endgroup\$
  • \$\begingroup\$ Can you be specific about what variable is null? It would be helpful if you posted the whole error from the Unity Log. \$\endgroup\$ – Honeybunch Oct 12 '16 at 2:23
  • \$\begingroup\$ @honeybunch My problem was that the game object don't have a audio source attached , once I create and attach it to the game object I solve the problem, thanks to bmac help \$\endgroup\$ – Gabriel Labarrera Oct 13 '16 at 12:54
1
\$\begingroup\$

You can't allocate components without attaching them to a GameObject (with gameObject.AddComponent). Unity will return a "null" object if you try to call a component constructor directly.

You should replace this line:

AudioSource audio = new AudioSource ();

With something like this:

AudioSource audio = GetComponent<AudioSource>();
if (audio == null) audio = gameObject.AddComponent<AudioSource>();

Which will use the existing AudioSource component on the object if you already made one, and will create and attach a new one if not.

Unrelatedly, you are unnecessarily casting the same object twice here (with (AudioClip) and as AudioClip):

audio.clip = (AudioClip)Resources.Load("GameOver") as AudioClip;

You only need one or the other, or even better:

audio.clip = Resources.Load<AudioClip>("GameOver");
\$\endgroup\$
  • \$\begingroup\$ Thanks, that solve my problem, at least at script, but still not hearing the sound. \$\endgroup\$ – Gabriel Labarrera Oct 11 '16 at 21:43
  • 1
    \$\begingroup\$ You can also use the static method AudioSource.PlayOneShot or PlayClipAtPoint to fire off one-off cues without explicitly creating an AudioSource instance first. This gives less control over the falloff settings than creating a custom AudioSource, but is often "good enough" for simple cues. \$\endgroup\$ – DMGregory Oct 11 '16 at 22:40
  • \$\begingroup\$ Yeah, i manage to reproduce the sound by using the method PlayOneShot, Thank you. \$\endgroup\$ – Gabriel Labarrera Oct 11 '16 at 23:49

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.