0

I'm making a game in Unity. I'm sure I'm correct and the code works, but this message appears. I want a clear reason and thank you

enter image description here

Line 165 is

GameManager.instance.state = GameManager.States.ROLL_DICE;

enter image description here

Here is my full code if needed

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Stone : MonoBehaviour 
{
    [Header("ROUTES")]
    public Route commonRoute;
    public Route finalRoute;

    public List<Node> fullRoute = new List<Node>();
    [Header("NODES")]
    public Node startNode;

    public Node baseNode;  // NOde in home

    public Node currentNode;

    public Node goalNode;

    int routePosition;
    int startNodeIndex;

    int steps; // DICE AMOUTE 
    int doneSteps;

    [Header("BOOLS")]

    public bool isOut;
    bool isMoving;

    bool hasTurn; // IS FOR HUMAN INPUT

    [Header("SELECTOR")]
    public GameObject selector;


    void Start()
    {
        startNodeIndex = commonRoute.RequestPosition(startNode.gameObject.transform);
        CreatFullRoute();

    }

    void CreatFullRoute()
    {
        for (int i = 0 ; i < commonRoute.childNodeList.Count ; i++)
        {
            int tempPos = startNodeIndex + i;
            tempPos %= commonRoute.childNodeList.Count;

            fullRoute.Add(commonRoute.childNodeList[tempPos].GetComponent<Node>());

        }

        for (int i = 0 ; i < finalRoute.childNodeList.Count ; i++)
        {
            
            fullRoute.Add(finalRoute.childNodeList[i].GetComponent<Node>());
            
        }

    }

    void Update()
    {
         if(Input.GetKeyDown(KeyCode.Space) && !isMoving)
         {
             steps = Random.Range(1, 7);
             Debug.Log("Dvice Number =" + steps);
             if(doneSteps + steps < fullRoute.Count)
             {
                 StartCoroutine(Move());
             }
             else
                 {
                     Debug.Log("Number is to high");
                 }
             }
    }

    IEnumerator Move()
    {
        if(isMoving)
        {
            yield break;
        }

        isMoving = true;

        while(steps>0)
        {
            // routePosition++;

            Vector3 nextPos = fullRoute[routePosition].gameObject.transform.position;

            while(MoveToNextNode(nextPos,8f)){yield return null;}
            yield return new WaitForSeconds(0.1f);
            steps--;
            doneSteps++;

        }
        isMoving = false;
    }

    bool MoveToNextNode(Vector3 goalPos, float speed)
    {
        return goalPos != (transform.position = Vector3.MoveTowards(transform.position,goalPos,speed * Time.deltaTime));
    }

    public bool ReturnIsOut()
    {
        return isOut;
    }

    public void LeaveBase()
    {
        steps = 1;
        isOut = true;
        routePosition = 0;
        // START COUROUTINE
        StartCoroutine(MoveOut());

    }

    IEnumerator MoveOut()
    {
        if(isMoving)
        {
            yield break;
        }

        isMoving = true;

        while(steps>0)
        {
            // routePosition++;

            Vector3 nextPos = fullRoute[routePosition].gameObject.transform.position;
            while(MoveToNextNode(nextPos,8f)){yield return null;}
            yield return new WaitForSeconds(0.1f);
            steps--;
            doneSteps++;

        }
        // UPDATE NODE
        goalNode = fullRoute[routePosition];

        // CHECK FOR CKICKING OTHER STONE

        if(goalNode.isTaken)
        {
            // RETURN TO START BASE NODE
        }

        goalNode.stone = this;
        goalNode.isTaken = true;

        currentNode = goalNode;
        goalNode = null;


        // REPORT BACK TO GAMEMANAGER
        
        GameManager.instance.state = GameManager.States.ROLL_DICE; // <-- THIS IS THE LINE
        isMoving = false;
    }
}
derHugo
  • 83,094
  • 9
  • 75
  • 115
JS MAN
  • 1
  • 2
    Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Absinthe Jan 24 '22 at 12:49
  • 1
    Well `I'm sure I'm correct and the code works, but this message appears` ... then I'm sure that you are not ^^ c# doesn't simply invent exceptions ;) sounds to me like `GameManager.instance` is `null` ... are you sure it exists in your scene and is initialized correctly? – derHugo Jan 24 '22 at 12:49

0 Answers0