0

when I load a new scene I get this error: NullReferenceException: Object reference not set to an instance of an object GameController+d__16.MoveNext () (at Assets/Scrips/GameController.cs:76) UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at :0)

this is my script to save:

using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.SceneManagement;
 
 public class Scene_Manager : MonoBehaviour
 {
     int Saved_scene;
     int Scene_index;
 
 
     public void Load_Saved_Scene()
     {
         Saved_scene = PlayerPrefs.GetInt("Saved");
 
         if (Saved_scene != 2)
             SceneManager.LoadSceneAsync(Saved_scene);
         else
             return;
     }
     public void Save_and_Exit()
     {
         Scene_index = SceneManager.GetActiveScene().buildIndex;
         PlayerPrefs.SetInt("Saved", Scene_index);
         PlayerPrefs.Save();
         SceneManager.LoadSceneAsync(0);
     }
     public void Next_Scene()
     {
         Scene_index = SceneManager.GetActiveScene().buildIndex + 1;
         SceneManager.LoadSceneAsync(Scene_index);
     }
 }

and this is my script for the whole game where I call save:

using System.Collections;
 using UnityEngine;
 using UnityEngine.SceneManagement;
 
 
 [RequireComponent(typeof(GameUI))]
 public class GameController : MonoBehaviour
 {
 
  
     public static GameController Instance { get; private set; }
 
     [SerializeField]
     private int knifeCount;
 
     [Header("Knife Spawning")]
     [SerializeField]
     private Vector2 knifeSpawnPosition;
     [SerializeField]
     
     private GameObject knifeObject;
 
     
     public GameUI GameUI { get; private set; }
 
     private void Awake()
     {
         
         Instance = this;
 
         GameUI = GetComponent<GameUI>();
     }
 
     private void Start()
     {
         
         GameUI.SetInitialDisplayedKnifeCount(knifeCount);
         
         SpawnKnife();
     }
 
     
     public void OnSuccessfulKnifeHit()
     {
         if (knifeCount > 0)
         {
             SpawnKnife();
         }
         else
         {
             StartGameOverSequence(true);
         }
     }
 
     
     private void SpawnKnife()
     {
         knifeCount--;
         Instantiate(knifeObject, knifeSpawnPosition, Quaternion.identity);
     }
 
    
     public void StartGameOverSequence(bool win)
     {
         StartCoroutine("GameOverSequenceCoroutine", win);
     }
 
        
     private IEnumerator GameOverSequenceCoroutine(bool win)
     {
         if (win)
         {
             
             yield return new WaitForSecondsRealtime(0.3f);
             FindObjectOfType<LevelLoader>().LoadNextLevel();
             FindObjectOfType<Scene_Manager>().Save_and_Exit();
         }
         else
         {
             GameUI.ShowRestartButton();
         }
     }
 
     public void RestartGame()
     {
         
         SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex, LoadSceneMode.Single);
     }
 }
Viw0a
  • 37
  • 4

1 Answers1

0

Now first of all take a look at this post to understand NullReferenceExceptions.

After that take a closer look at your error message:

NullReferenceException: Object reference not set to an instance of an object GameController+d__16.MoveNext () (at Assets/Scrips/GameController.cs:76)

The (at Assets/Scrips/GameController.cs:76) part tells you exactly where you error is thrown (which basically means where it occured). It's in your Assets/Scrips/GameController.cs script at line 76.

private IEnumerator GameOverSequenceCoroutine(bool win)
{
    if (win)
    {     
        yield return new WaitForSecondsRealtime(0.3f);
        FindObjectOfType<LevelLoader>().LoadNextLevel();
        FindObjectOfType<Scene_Manager>().Save_and_Exit(); // <--- HERE
    }
     ...
}

In your specific implementation my best guess would be that you shouldn't load your new scene before you try to save & quit. You're starting to load your new scene asynchronously and then call another function which loads another scene (?) while not actually preventing the first scene from activating until your other functions actually executed properly. There's a plethora of cases where this can and will break.

I think you'll need to rethink what it is your actually trying to accomplish because these function calls don't make sense like that.

Nicolas
  • 440
  • 4
  • 13