Search Unity

[SOLVED] 2D Routlike Tutorial - NullReferenceException in my GameManager

Discussion in 'Scripting' started by lazy-fox, May 17, 2019.

  1. lazy-fox

    lazy-fox

    Joined:
    Feb 2, 2019
    Posts:
    14
    I am currently doing the 2D Roguelike Tutorial from the official tutorial section
    https://unity3d.com/learn/tutorials/s/2d-roguelike-tutorial

    I have completed all the videos in sections 1-3.

    The game should be functional at this point. Mine is not. I am running into a compiler error.

    I'm having trouble understanding the following error. My code looks exactly like the tutorials does up at that point. At least as far as I can tell.

    I know what the error is supposed to men. It is supposed to tell you that one needs to create an instance of another script in order for the current script to be used by it. But I don't see how that is an issue in my GameManager. Any ideas?


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5.  
    6. public class GameManager : MonoBehaviour
    7. {
    8.     public float turnDelay = 0.1f;
    9.     public static GameManager instance = null;
    10.     public BoardManager boardScript;
    11.     public int playerFoodPoints = 100;
    12.     [HideInInspector] public bool playersTurn = true;
    13.    
    14.     private int level = 3;
    15.     private List<Enemy> enemies;
    16.     private bool enemiesMoving;
    17.    
    18.     // Start is called before the first frame update
    19.     void Awake()
    20.     {
    21.         if(instance == null)
    22.             instance = this;
    23.         else if(instance != this)
    24.             Destroy(gameObject);
    25.        
    26.         DontDestroyOnLoad(gameObject);
    27.         enemies = new List<Enemy>();
    28.         boardScript = GetComponent<BoardManager>();
    29.         InitGame();
    30.     }
    31.    
    32.     void InitGame()
    33.     {
    34.         enemies.Clear();
    35.         boardScript.SetupScene(level);
    36.     }
    37.    
    38.     public void GameOver()
    39.     {
    40.             enabled = false;
    41.     }
    42.  
    43.     // Update is called once per frame
    44.     void Update()
    45.     {
    46.    
    47.         if (playersTurn || enemiesMoving)
    48.             return;
    49.         StartCoroutine(MoveEnemies());
    50.        
    51.     }
    52.    
    53.     public void AddEnemyToList(Enemy script)
    54.     {
    55.         enemies.Add(script);
    56.     }
    57.    
    58.     IEnumerator MoveEnemies()
    59.     {
    60.        
    61.         enemiesMoving = true;
    62.         yield return new WaitForSeconds(turnDelay);
    63.         if(enemies.Count == 0)
    64.             {
    65.                 yield return new WaitForSeconds(turnDelay);
    66.             }
    67.            
    68.             for(int i = 0; i < enemies.Count; i++)
    69.             {
    70.                 enemies[i].MoveEnemy();
    71.                 yield return new WaitForSeconds(enemies[i].moveTime);
    72.             }
    73.            
    74.             playersTurn = true;
    75.     }
    76. }
    77.  
    78.  
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    6,489
    There's some sort of mismatch here. According to the error message pasted, the error is happening in a method named OnSceneLoaded on line 62, but the pasted script has no such method and line 62 is in the MoveEnemies coroutine, which doesn't make any sense.

    When you double-click on the error, does it take you to a file that looks exactly like the file pasted here? If so, which line is highlighted? (Seems like it should be the "yield return..." line)
     
  3. lazy-fox

    lazy-fox

    Joined:
    Feb 2, 2019
    Posts:
    14
    SOLVED

    Problem.
    Code (CSharp):
    1. public BoardManager boardScript;
    Solution

    Code (CSharp):
    1. //set public to private
    2. private BoardManager boardScript;