0

I Got Error on 2nd if statement what is the problem

public void Play()
    {

        ButtonClickSound();
        int sceneNumber = PlayerPrefs.GetInt("LevelNumber");
        if (sceneNumber > 17)
            sceneNumber = Random.Range(2, 18);
        SceneManager.LoadSceneAsync(sceneNumber);   
        if(SceneManager.GetActiveScene().buildIndex != 17)
        {
            CanvasManager.instance.endlessPanel.SetActive(true);
        }
    }

Error is:

Null reference Exception object Reference not set to instance of object
mousetail
  • 7,009
  • 4
  • 25
  • 45
  • If this is UnityScript, why have you also tagged C#? They're two different programming languages. UnityScript has been deprecated for a long time, so new Unity development should be done in C# now. Also, for any questions involving errors, you should also include the full error details (as text in the question). – ProgrammingLlama May 31 '23 at 05:43
  • error is NullRefferenceException object reffernce not to set to instance of object – Azeem Khan May 31 '23 at 06:04
  • Ah, so it is C# and not UnityScript. I didn't notice the `public void Play()`. Essentially the error means that something is `null`. You need to find out what is `null` and then find out why. – ProgrammingLlama May 31 '23 at 06:06
  • nothing is null the panel is set to its refference but level loads and panel not load – Azeem Khan May 31 '23 at 06:09
  • 2
    Why did you delete your code? Anyway, set a breakpoint and debug the code. FInd out what is `null`. I'd be suspicious of `instance`, and `endlessPanel` so check if they have values if they're null. – ProgrammingLlama May 31 '23 at 06:11
  • i edit the question again now what can i do can you tell me breifly? – Azeem Khan May 31 '23 at 06:20
  • Debug. Find out what's null. Then try to find out why. Once you know why, find out how to fix it so that it's not null. Now your problem is resolved. It's the 5-step process that never fails. – ProgrammingLlama May 31 '23 at 07:08
  • ok let me check – Azeem Khan May 31 '23 at 07:16

0 Answers0