0

I am new to Unity and c#. I am trying to create a dice game that rolls two dices and the total values of the two dice faces is added to the player's score. The game continues until the player rolls two 1's or the total score reached or exceeds 50.

However, I can't manage display updated score after rolling the dice, and I keep having this NullReferenceException error. I can't understand why. Please help experts!!

Here's my Dice code

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

public class Dice : MonoBehaviour
{
    Rigidbody rb;
    bool hasLanded;
    bool thrown;
    Vector3 initPosition;
    public int diceValue;

    public DiceSide[] diceSides;

    Check check;

    

    void Start()
    {
        check = GameObject.Find("Canvas").GetComponent<Check>();

        rb = GetComponent<Rigidbody>();
        initPosition = transform.position;
        rb.useGravity = false;

    }

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            RollDice();
            check.scoreUpdated = false;
        }

        if(rb.IsSleeping() && !hasLanded && thrown)
        {
            hasLanded = true;
            rb.useGravity = false;
            rb.isKinematic = true;

            SideValueCheck();

        }
        else if(rb.IsSleeping() && hasLanded && diceValue == 0)
        {
            RollAgain();
        }
    }

    void RollDice()
    {
        if(!thrown && !hasLanded)
        {
            thrown = true;
            rb.useGravity = true;
            rb.AddTorque(Random.Range(0, 500), Random.Range(0, 500), Random.Range(0, 500));
        }
        else if(thrown && hasLanded)
        {
            Reset();
        }

    }

    void Reset()
    {
        transform.position = initPosition;
        thrown = false;
        hasLanded = false;
        diceValue = 0;
        rb.useGravity = false;
        rb.isKinematic = false;
    }

    void RollAgain()
    {
        Reset();
        thrown = true;
        rb.useGravity = true;
        rb.AddTorque(Random.Range(0, 500), Random.Range(0, 500), Random.Range(0, 500));

    }

    void SideValueCheck()
    {
        diceValue = 0;
        foreach (DiceSide side in diceSides)
        {
            if (side.OnGround())
            {
                diceValue = side.sideValue;
                Debug.Log(diceValue + " has been rolled!");

            }
        }

    }




}

and here's my DiceSide Code.

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

public class DiceSide : MonoBehaviour
{
    bool onGround;
    public int sideValue;

    void OnTriggerStay(Collider col)
    {
        if(col.tag == "Ground")
        {
            onGround = true;
        }
    }

    void OnTriggerExit(Collider col)
    {
        if(col.tag == "Ground")
        {
            onGround = false;
        }
    }

    public bool OnGround()
    {
        return onGround;
    }
}

Lastly, Check Code- Which is the part I am having errors.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;

public class Check : MonoBehaviour
{

    Dice dice;
    Dice dice2;

    public int dicevalue1;
    public int dicevalue2;
    private int score;
    public int totalScore;

    public TMP_Text Score;
    public TMP_Text TotalScore;
    public TMP_Text Win;
    public TMP_Text Lose;
    public TMP_Text Player;

    public bool scoreUpdated;


    void Start()
    {
        score = 0;
        totalScore = 0;
        Win.text = "";
        Lose.text = "";
        dicevalue1 = 0;
        dicevalue2 = 0;
        scoreUpdated = false;
    }

    void Awake()
    {
        dice = GameObject.Find("Dice").GetComponent<Dice>();
        dice2 = GameObject.Find("Dice2").GetComponent<Dice>();
    }

    void SetScoreText()
    {
        if (dicevalue1 == 1 && dicevalue2 == 1)
        {
            Lose.text = "You Lose:("; //displays text when player rolls two 1's
        }
        else if (totalScore >= 50)
        {
            Win.text = "You win!"; //displays win text when totalscore(accumulated) reaches or exceeds 50
        }
        else
        {
            TotalScore.text = totalScore.ToString();
        }
    }


    public void UpdateScore()
    {
        dicevalue1 = dice.diceValue;
        dicevalue2 = dice2.diceValue;
        //stores diceValues
        if (!scoreUpdated && dicevalue1 != 0 && dicevalue2 != 0)
        {
            score = dicevalue1 + dicevalue2;
            Debug.Log(score);
            totalScore += score;
            Debug.Log(totalScore);
            scoreUpdated = true;
            SetScoreText();
        }

    }
}
Chloe Jung
  • 11
  • 1

0 Answers0