0

object reference not sent to an instance of an object My code is SceneFader.instance.LoadLevel("Gameplay"); I am using it for my new 2d android game in unity.

my complete code for MainMenuController.cs is '

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

public class MainMenuController : MonoBehaviour {

    [SerializeField]
    private Button musicBtn;

    [SerializeField]
    private Sprite[] musicIcons;

    // Use this for initialization
    void Start () {
        CheckToPlayTheMusic ();
    }

    void CheckToPlayTheMusic(){
        if (GamePreferences.GetMusicState () == 1) {
            MusicController.instance.PlayMusic (true);
            musicBtn.image.sprite = musicIcons [1]; 
        } else {
            MusicController.instance.PlayMusic (false);
            musicBtn.image.sprite = musicIcons [0];
        }

    }

    public void StartGame(){
        GameManager.instance.gameStartedFromMainMenu = true;
        //Application.LoadLevel ("Gameplay");
        SceneFader.instance.LoadLevel("Gameplay");
    }

    public void HighscoreMenu(){
        Application.LoadLevel ("HighScoreScene");


    }

    public void OptionsMenu(){
        Application.LoadLevel ("OptionsMenu");
    }


    public void QuitGame(){
        Application.Quit ();

    }


    public void MusicButton(){
        if (GamePreferences.GetMusicState () == 0) {
            GamePreferences.SetMusicState (1);
            MusicController.instance.PlayMusic (true);
            musicBtn.image.sprite = musicIcons [1];
        } else if (GamePreferences.GetMusicState () == 1) {
            GamePreferences.SetMusicState (0);
            MusicController.instance.PlayMusic (false);
            musicBtn.image.sprite = musicIcons [0];
        }

    }

}

Any body have same kind of issue?

please check scenefader script

using UnityEngine; using System.Collections;

public class SceneFader : MonoBehaviour {

public static SceneFader instance;

[SerializeField]

private GameObject fadePanel;

[SerializeField]
private Animator fadeAnim;

// Use this for initialization
void Awake () {
    MakeSingleton ();
}

void MakeSingleton(){
    if (instance != null) {
        Destroy (gameObject);
    } else {
        instance = this;
        DontDestroyOnLoad (gameObject);

    }

}

public void LoadLevel(string level){
    StartCoroutine (FadeInOut (level));
}

IEnumerator FadeInOut (string level){
    fadePanel.SetActive (true);
    fadeAnim.Play ("FadeIn");

    yield return StartCoroutine (MyCoroutine.WaitForRealSeconds (1f));

    Application.LoadLevel (level);
    fadeAnim.Play ("FadeOut");

    yield return StartCoroutine (MyCoroutine.WaitForRealSeconds (.7f));

    fadePanel.SetActive (false);

}

}

  • Please, add more info like error is showing (copy past error), where it says is the error, etc... – matiaslauriti Jun 18 '16 at 12:34
  • I changed the title so that it wont be closed. Don't use that error as the title because it is a duplicate if you do so. You said it crashes at `SceneFader.instance.LoadLevel` line. Post your `SceneFader` script for further help. – Programmer Jun 18 '16 at 12:37
  • It could be that maybe he didn't add the scene to the build. It's a common error, very easy to forget. – matiaslauriti Jun 18 '16 at 12:46
  • @P0lT10n Not really true. He would get this error `Scene 'Gameplay' (-1) couldn't be loaded because it has not been added to the build settings or the AssetBundle has not been loaded.`. – Programmer Jun 18 '16 at 12:51
  • You are right. My mistake. I am pretty sure he didn't initialized SceneManager. So when calling that Object, the singleton is not initialized (because the .instance is not auto creating a new instance) – matiaslauriti Jun 18 '16 at 12:54
  • i have already added to the build setting.... – Xiaurrehman Xee Jun 18 '16 at 16:23
  • Did you read my comment that says post your `SceneFader` script? – Programmer Jun 18 '16 at 16:45
  • please check now.. i have edited my question and have added scenefader script.. and the error . – Xiaurrehman Xee Jun 18 '16 at 16:51
  • all other options of MainMenu are working fine. only when i click on start game then this error comes – Xiaurrehman Xee Jun 18 '16 at 17:01

1 Answers1

0

Problem is due to singleton. There is no instance of SceneFader.

Change:

void MakeSingleton(){
    if (instance != null) {
        Destroy (gameObject);
    } else {
        instance = this;
        DontDestroyOnLoad (gameObject);
}

to

void MakeSingleton(){
    if (instance != null && instance != this) {
        Destroy (gameObject);
    } else {
        instance = this;
        DontDestroyOnLoad (gameObject);
}

That should solve your problem. If this problem is still there then you have to remove singleton from your project. There is something called GetComponent. Use it to get instance of your SceneFader script.

Create a GameObject called SceneFaderObj and attach the SceneFader script to it from the Editor. Don't forget to remove all the singleton code in your SceneFader script.

Now, in your MainMenuController script,

SceneFader screenFader;
void Start () {
    //Find the SceneFaderObj GameObject
    GameObject tempObj = GameObject.Find("SceneFaderObj");
    //Get the SceneFader script instance that is attached to SceneFaderObj 
    screenFader = tempObj.GetComponent<SceneFader>();
}

You can then use screenFader to call functions in SceneFader script. Do the-same thing for your other scripts such as MusicController and GamePreferences and GameManager scripts.

Programmer
  • 121,791
  • 22
  • 236
  • 328
  • sorry but i have same error after doing all this.. all the other things are working good. only getting error when i click to start game. – Xiaurrehman Xee Jun 18 '16 at 20:37
  • @XiaurrehmanXee **"sorry but i have same error after doing all this"** You don't have to do all those. All you have to do is change `if (instance != null) {` to `if (instance != null && instance != this) {`. If that doesn't work then you have to re-do your work with the second part of my answer. Some new Unity users jump to using static right away. You will have problems with this. Simply use `GetComponent` and remove all your `static` stuff. I can't help any further if you can't do that. It doesn't matter if all the other things are working good or not. – Programmer Jun 18 '16 at 20:43