0

I am currently stuck on getting an error that is making it hard to figure out what is wrong. As you can tell I'm really new to this. Basically my code error says this

nullreferenceexcpetion: object refence not set to an instance of an object 
NewBehaviourScript.start () (at location of file blah blah blah)

this is the code i wrote:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class NewBehaviourScript : MonoBehaviour
{
    public GameObject soundControlButton;
    public Sprite hie;
    public Sprite nie;


    // Start is called before the first frame update
    void Start()
    {
        if (AudioListener.pause == true)
        {
            soundControlButton.GetComponent<Image>().sprite = hie;
        }
        else
        {
            soundControlButton.GetComponent<Image>().sprite = nie;
        }

    }

    // Update is called once per frame
    void Update()
    {

    }

    public void SoundControl()
    {
        if (AudioListener.pause == true)
        {
            AudioListener.pause = false;
            soundControlButton.GetComponent<Image>().sprite = nie;
        }
        else
        {
            AudioListener.pause = true;
            soundControlButton.GetComponent<Image>().sprite = hie;
        }
    }
}

thank you so much for any help that can be given this is so important for me

  • You never declare or pull in an instance of `AudioListener` in your code. – Tim Hunter Jun 21 '21 at 20:43
  • "at location of file blah blah blah" - the "blah blah blah" part usually tells you either the exact line where the error happened, or sometimes a line before or after it. The "null reference" thing means that some variable doesn't have a value assigned to it (it's "null"), and you're trying to do something with it, or access something within it. In your case, it looks like it's either `AudioListener`, or `soundControlButton`, or if not that, then the call to GetComponent returned nothing (couldn't find the image component) `soundControlButton.GetComponent()` – Filip Milovanović Jun 21 '21 at 20:45

0 Answers0