NullReferenceException: Object reference not set to an instance of an object

Can some one help me with this, I am fairly new to coding. I get this exception on an OnMouseDown event. I have 3 events in each 3 different game mode. If I hit play on a mode and then switch to playing a different mode, then this error occurs. How can I solve it?

I will be building it to an android device.

Coding in C#

The error:

NullReferenceException: Object reference not set to an instance of an object
getMusic.get_getMusic_Instance () (at F:/…/getMusic.cs:40)
getCenter+c__Iterator0.MoveNext () (at F:/…/getCenter.cs:23)
UnityEngine.SendMouseEvents:smile:oSendMouseEvents(Int32, Int32)

thanks in anticipation :slight_smile:

You need to actually post the script that’s causing the error, which in this case is “getMusic.cs”. Please remember to use [code ][/code ] tags as well, so that it’s easier to read.

using UnityEngine;
using System.Collections;
//using UnityEditor;
using System;
using SongSelectorDLL;

public class getMusic : MonoBehaviour
{

  static int score = 1;
  static float delay = 3.0f;
  static int n = 0;
  static float wait;
  static double counter;
  static double newCounter;
  //public GameObject Target_01;
  //public GameObject SpecialObject;
  GameObject temp, temp01, stemp;
  static Boolean exitFlag = false;
  Boolean exitFlagReset = false;
  //public Audio a;
  public AudioSource audioSource;
  public static getMusic m;
  static int speedCounter = 0;
  int speedCounterReset = 0;
  public static bool isWorking = true;
  bool isWorkingReset = true;
   
  //GameObject audiogetMusic;
  private static getMusic instance;

  public static getMusic getMusic_Instance
  {
  get
  {
  if (instance == null)
  {
  instance = GameObject.FindObjectOfType<getMusic>();
  DontDestroyOnLoad(instance.gameObject);
  }

  return instance;
  }
  }
   
  // Use this for initialization
  void Start()
  {
  GetComponent<AudioSource>().spatialBlend = 0.0f;
  GetComponent<AudioSource>().panStereo = 0;
  //wait = 8.0f;
  score = 1;
  //audiogetMusic = GameObject.Find("SoundPicker");
  audioSource = gameObject.GetComponent<AudioSource>();
  audioSource.clip = GetComponent<AudioSource>().clip;
   
  //Vector2 position = new Vector2(UnityEngine.Random.Range(-8.5f, -4.3f), UnityEngine.Random.Range(-4.3f, 4.3f));
  //Instantiate(Target_01, position, Quaternion.identity);
  //Vector2 position01 = new Vector2(UnityEngine.Random.Range(4.3f, 8.5f), UnityEngine.Random.Range(-4.3f, 4.3f));
  //Instantiate(Target_02, position01, Quaternion.identity);
  if (m == null)
  m = this.GetComponent<getMusic>();

  //PlayMusic();
  //audioSource.Play();
  }
   
  public void makeZero()
  {
  GetComponent<AudioSource>().panStereo = 0;
  }
  void Awake()
  {
  resetVariables();
  //audio.Stop();
  //if (TryAgain.normalMode == true)
  //{
  //GetComponent<AudioSource>().Play();
  //}

  if (instance == null)
  {
  //If I am the first instance, make me the Singleton
  instance = this.GetComponent<getMusic>();
  DontDestroyOnLoad(this);
  }
  else
  {
  //If a Singleton already exists and you find
  //another reference in scene, destroy it!
  if (this != instance)
  Destroy(this.gameObject);
  }
  }

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

  //Debug.Log("Audio Len: " + len);
  //Debug.Log("Music: "+audioSource.time);
  //Debug.Log("Sudden Death: " + suddenDeath);
  //Debug.Log("Wait: " + wait);
  //len = audioSource.time;
  //if (SelectSong.startGame == true)
  //{
  //  if (!timer01Flag)
  //  {
  //  counter -= Time.deltaTime;
  //  }
  //  newCounter -= Time.deltaTime;
  //}
  //else if (TryAgain.normalMode == true)
  //{
  //  if (!timer01Flag)
  //  {
  //  counter -= Time.deltaTime;
  //  }
  //  newCounter -= Time.deltaTime;
  //}
  //newCounter02 -= Time.deltaTime;
  //newCounter03 -= Time.deltaTime;
  //if (!timer01Flag)
  //{
  //  if (waitFor && counter <= 0)
  //  {
  //  Destroy(temp);
  //  Destroy(temp01);
  //  waitFor = false;
  //  counter = 5;

  //  }
  //}
  //if (specialWaitFor && newCounter <= 0)
  //{
  //  Destroy(stemp);
  //  specialWaitFor = false;
  //  newCounter = UnityEngine.Random.Range(7, 15);
  //  specialDelay = false;
  //}

  //if (counter <= 0 && !waitFor) {
  //  var pos = new Vector2(UnityEngine.Random.Range(-8.7f, 8.7f), UnityEngine.Random.Range(-4.7f, 4.7f));
  //  var rot = Quaternion.identity;
  //  temp = (GameObject)Instantiate(Target_01,pos,rot);
  //  temp.AddComponent("Music");
  //  counter = 2.0;
  //  waitFor = true;
  //   
  //}
  //if (counter <= 0 && !waitFor)
  //{
  //  var pos = new Vector2(UnityEngine.Random.Range(-8.5f, -4.3f), UnityEngine.Random.Range(-4.3f, 4.3f));
  //  var rot = Quaternion.identity;
  //  temp = (GameObject)Instantiate(Target_01, pos, rot);
  //  temp.AddComponent("Music");
  //  counter = 2.0f;
  //  waitFor = true;
  //}

  //if (newCounter <= 0 && !specialWaitFor)
  //{
  //  var pos = new Vector2(UnityEngine.Random.Range(-8.7f, 8.7f), UnityEngine.Random.Range(-4.7f, 4.7f));
  //  var rot = Quaternion.identity;
  //  stemp = (GameObject)Instantiate(SpecialObject, pos, rot);
  //  stemp.AddComponent("specialScript");
  //  newCounter = 5.0;
  //  specialWaitFor = true;
  //  specialWait = 5.0f;
  //}

  // Debug.Log("Delta Time: "+Time.deltaTime);
  //if (!generateBonus.timer01Flag)
  //{
  //if (SelectSong.startGame == true)
  //{
  //  //Debug.Log(generateBonus.timer01Flag);
  //  if (!exitFlag)
  //  wait -= Time.deltaTime;
  //  if (wait <= 0 && !exitFlag)
  //  {
  //  //Debug.Log("Time limit exceeded: Quit : " + wait + "  Score : " + (ScoreNumber.Score + Music.score + Music01.score));
  //  exitFlag = true;
  //  StopMusic();
  //  //GetComponent<AudioSource>().Stop();
  //  //Caching.CleanCache();
  //  //Application.Quit();
  //  isWorking = false;
  //  Application.LoadLevel(3);
  //  }
  //}
  //if (TryAgain.normalMode == true)
  //{
  //  //Debug.Log(generateBonus.timer01Flag);
  //  if (!exitFlag)
  //  wait -= Time.deltaTime;
  //  if (wait <= 0 && !exitFlag)
  //  {
  //  Debug.Log("Time limit exceeded: Quit : ");
  //  exitFlag = true;
  //  StopMusic();
  //  //GetComponent<AudioSource>().Stop();
  //  //Caching.CleanCache();
  //  //Application.Quit();
  //  isWorking = false;
  //  Application.LoadLevel(3);
  //  }
  //}

   
  //if (Input.GetKeyDown(KeyCode.RightArrow) && !exitFlag)
  //{
  //  if (audio.pan == 1)
  //  {
  //  score++;
  //  Debug.Log("Right : " + score);
  //  new WaitForSeconds(getDelay(score));
  //  n = random();
  //  if (n == 1)
  //  audio.pan = 1;
  //  else if (n == 0) audio.pan = 0;
  //  else audio.pan = -1;
  //  }
  //  else
  //  {
  //  audio.Stop();
  //  Debug.Log("Right exit : Score: " + (score + NewBehaviourScript.score));
  //  Application.Quit();
  //  }
  //}
  //else if (Input.GetKeyDown(KeyCode.LeftArrow) && !exitFlag)
  //{
  //  if (audio.pan == -1)
  //  {
  //  score++;
  //  Debug.Log("Left : " + score);
  //  new WaitForSeconds(getDelay(score));
  //  n = random();
  //  if (n == 1)
  //  audio.pan = 1;
  //  else if (n == 0) audio.pan = 0;
  //  else audio.pan = -1;
  //  }
  //  else
  //  {
  //  audio.Stop();
  //  Debug.Log("Left exit : Score: "+(score+NewBehaviourScript.score));
  //  Application.Quit();
  //  }
  //}
  //else if (Input.GetKeyDown(KeyCode.UpArrow) && !exitFlag)
  //{
  //  if (audio.pan == 0)
  //  {

  //  score++;
  //  Debug.Log("Up : " + score);
  //  new WaitForSeconds(getDelay(score));
  //  n = random();
  //  if (n == 1)
  //  audio.pan = 1;
  //  else if (n == 0) audio.pan = 0;
  //  else audio.pan = -1;
  //  }
  //  else
  //  {
  //  audio.Stop();
  //  Debug.Log("Up exit: Score: "+(score + NewBehaviourScript.score));
  //  Application.Quit();
  //  }
  //}
  //}
  }

  public static void eventOccured(string code) {
  if(m==null) m = new getMusic();
  m.doSomething(code);
   
  }
  public int random()
  {
  return UnityEngine.Random.Range(1, 12) % 3;
  }

  public void doSomething(string code) {
  if (SelectSong.startGame == true)
  {
  switch (code)
  {

  case "HowToBrain": if (!exitFlag)
  {
  if (audioSource.panStereo == 0)
  {

  ScoreNumber.Score++;
  speedCounter++;
  //Debug.Log("Up : " + score);
  //new WaitForSeconds(getDelay(score));
  n = random();
  if (n == 1)
  audioSource.panStereo = 1;
  else if (n == 0) audioSource.panStereo = 0;
  else audioSource.panStereo = -1;

  }
  //  else
  //  {
  //  Wrong.wrongCount++;
  //  if (Wrong.wrongCount == 3)
  //  {
  //  StopMusic();
  //  //audioSource.Stop();
  //  //Debug.Log("Up exit: Score: " + (score + NewBehaviourScript.score));
  //  //Application.Quit();
  //  isWorking = false;
  //  Application.LoadLevel(3);
  //  }
  //  }
  }
  break;
  case "HowToRightBrain": if (!exitFlag)
  {
  if (audioSource.panStereo == 1)
  {
  ScoreNumber.Score++;
  speedCounter++;
  //Debug.Log("Right : " + score);
  //new WaitForSeconds(getDelay(score));

  n = random();
  if (n == 1)
  audioSource.panStereo = 1;
  else if (n == 0) audioSource.panStereo = 0;
  else audioSource.panStereo = -1;
  }
  //  else
  //  {
  //  Wrong.wrongCount++;
  //  if (Wrong.wrongCount == 3)
  //  {
  //  StopMusic();
  //  //audioSource.Stop();
  //  //Debug.Log("Right exit : Score: " + (score + NewBehaviourScript.score));
  //  //Application.Quit();
  //  isWorking = false;
  //  Application.LoadLevel(3);
  //  }
  //  }
  }
  break;
  case "HowToLeftBrain": if (!exitFlag)
  {
  if (audioSource.panStereo == -1)
  {
  ScoreNumber.Score++;
  speedCounter++;
  //Debug.Log("Left : " + score);
  //new WaitForSeconds(getDelay(score));
  n = random();
  if (n == 1)
  audioSource.panStereo = 1;
  else if (n == 0) audioSource.panStereo = 0;
  else audioSource.panStereo = -1;
  }
  //else
  //{
  //  Wrong.wrongCount++;
  //  if (Wrong.wrongCount == 3)
  //  {
  //  StopMusic();
  //  //audioSource.Stop();
  //  //Debug.Log("Left exit : Score: " + (score + NewBehaviourScript.score));
  //  //Application.Quit();
  //  isWorking = false;
  //  Application.LoadLevel(3);
  //  }
  //}
  }
  break;
  }
  }
  }

  //else if (TryAgain.normalMode == true)
  //{
  //  switch (code)
  //  {

  //  case "Brain": if (!exitFlag)
  //  {
  //  if (audioSource.panStereo == 0)
  //  {

  //  ScoreNumber.Score++;
  //  speedCounter++;
  //  //Debug.Log("Up : " + score);
  //  //new WaitForSeconds(getDelay(score));
  //  n = random();
  //  if (n == 1)
  //  audioSource.panStereo = 1;
  //  else if (n == 0) audioSource.panStereo = 0;
  //  else audioSource.panStereo = -1;

  //  }
  //  //else
  //  //{
  //  //  Wrong.wrongCount++;
  //  //  if (Wrong.wrongCount == 3)
  //  //  {
  //  //  //StopMusic();
  //  //  //audioSource.Stop();
  //  //  //Debug.Log("Up exit: Score: " + (score + NewBehaviourScript.score));
  //  //  //Application.Quit();
  //  //  //isWorking = false;
  //  //  Application.LoadLevel(3);
  //  //  }
  //  //}
  //  }
  //  break;
  //  case "RightBrain": if (!exitFlag)
  //  {
  //  if (audioSource.panStereo == 1)
  //  {
  //  ScoreNumber.Score++;
  //  speedCounter++;
  //  //Debug.Log("Right : " + score);
  //  //new WaitForSeconds(getDelay(score));

  //  n = random();
  //  if (n == 1)
  //  audioSource.panStereo = 1;
  //  else if (n == 0) audioSource.panStereo = 0;
  //  else audioSource.panStereo = -1;
  //  }
  //  //else
  //  //{
  //  //  Wrong.wrongCount++;
  //  //  if (Wrong.wrongCount == 3)
  //  //  {
  //  //  StopMusic();
  //  //  //audioSource.Stop();
  //  //  //Debug.Log("Right exit : Score: " + (score + NewBehaviourScript.score));
  //  //  //Application.Quit();
  //  //  isWorking = false;
  //  //  Application.LoadLevel(3);
  //  //  }
  //  //}
  //  }
  //  break;
  //  case "LeftBrain": if (!exitFlag)
  //  {
  //  if (audioSource.panStereo == -1)
  //  {
  //  ScoreNumber.Score++;
  //  speedCounter++;
  //  //Debug.Log("Left : " + score);
  //  //new WaitForSeconds(getDelay(score));
  //  n = random();
  //  if (n == 1)
  //  audioSource.panStereo = 1;
  //  else if (n == 0) audioSource.panStereo = 0;
  //  else audioSource.panStereo = -1;
  //  }
  //  //else
  //  //{
  //  //  Wrong.wrongCount++;
  //  //  if (Wrong.wrongCount == 3)
  //  //  {
  //  //  StopMusic();
  //  //  //audioSource.Stop();
  //  //  //Debug.Log("Left exit : Score: " + (score + NewBehaviourScript.score));
  //  //  //Application.Quit();
  //  //  isWorking = false;
  //  //  Application.LoadLevel(3);
  //  //  }
  //  //}
  //  }
  //  break;
  //  }
  //}
  //}



  //public float getDelay(int score)
  //{

  //  if (generateBonus.specialDelay) return 3.0f;

  //  if (speedCounter < 20)
  //  {
  //  //counter = 0.0;

  //  {
  //  wait = 8.0f;
  //  Timer.Timeit = wait;
  //  }

  //  //if (Timer.Timeit >= 5)
  //  //{
  //  //  Timer.Timeit = 5;
  //  //}

  //  return 3.0f;
  //  }

  //  if (speedCounter > 20 && speedCounter < 50)
  //  {
  //  //counter = 0.0;


  //  {
  //  wait = 6.0f;
  //  Timer.Timeit = wait;
  //  }
  //  //if (Timer.Timeit >= 4)
  //  //{
  //  //  Timer.Timeit = 4;
  //  //}
  //  return 3.0f;
  //  }
  //  if (speedCounter > 50 && speedCounter < 80)
  //  {
  //  //counter = 0.0;


  //  {
  //  wait = 3.0f;
  //  Timer.Timeit = wait;
  //  }
  //  //if (Timer.Timeit >= 2)
  //  //{
  //  //  Timer.Timeit = 2;
  //  //}
  //  return 3.0f;
  //  }
  //  if (speedCounter > 80)
  //  {
  //  //counter = 0.0;


  //  {
  //  wait = 1.0f;
  //  Timer.Timeit = wait;
  //  }
  //  /*if (Timer.Timeit >= 0.5)
  //  {
  //  Timer.Timeit = 0.5;
  //  }*/
  //  return 3.0f;
  //  }
  //  return 3.0f;
  //}

  public void PlayMusic()
  {
  SongSelector.GetInstance.PlayMusic();
  }

  //Call this before changing the level (i.e before loading the tryagain level)
  //public void StopMusic()
  //{
  //  SongSelector.GetInstance.StopMusic();

  //}
  void resetVariables()
  {
  score = 1;
  delay = 3.0f;
  n = 0;
  exitFlag = exitFlagReset;
  //public Audio a;
  AudioSource audioSource;
  getMusic m;
  GetComponent<AudioSource>().spatialBlend = 0.0f;
  GetComponent<AudioSource>().panStereo = 1;
  //wait = 8.0f;
  //specialWait = 5.0f;
  counter = 5.0;
  newCounter = 7.0;
  score = 1;
  audioSource = gameObject.GetComponent<AudioSource>();
  audioSource.clip = GetComponent<AudioSource>().clip;
  m = getMusic_Instance;
  speedCounter = speedCounterReset;
  isWorking = isWorkingReset;
  }
}

Code is a bit hard to guess where your line 40 is as there are a lot of annoying newlines, but make sure to check whether ‘instance’ is null. Also, why do you do: instance = GameObject.FindObjectOfType<GetMusic>(); As you are making a singleton using instance = this; should be enough.

Also check whether you have attached a AudioSource to your gameobject, and use the [RequireComponent] attribute to force one to be available at all times: Unity - Scripting API: RequireComponent

I have edited the code so that it will be easy to find the line. Meanwhile, I will try what you said.

I have posted the code