do something based on dice results

I’m totally new to any sort of programming. I’ve been going through tutorials and it’s helping minimally. That’s all to say I probably won’t understand what you’re saying but maybe I have learnt enough to figure it out.

I’m working on a board game and need help with the dice rolling part.

I have two d6 dice and they are working correctly. Now I need to make things happen based on the results. How do I do that?

One dice determines what piece can be placed. And the other dice determines what region of the board it can be placed in. Where do I begin? Is there a template I can’ use or a tutorial? I’ve done lots of google and youtube searching for my question here and really haven’t found what I’m looking for.

Thanks for any help.

Do you already have some code, or are the dice only simulated through physics? How to approach this will depend on what you already have and how it’s structured. If you have some relevant scripts, post them (with code tags) so that we can get a better idea about where to point you.

This could mean… almost anything. Do you have dice being rolled with physics? Do you have a random number generator? What are your dice here and what do they do?

 using System.Collections;
using UnityEngine;

public class ColorDice : MonoBehaviour
{

    // Array of dice sides sprites to load from Resources folder
    private Sprite[] diceSides;

    // Reference to sprite renderer to change sprites
    private SpriteRenderer rend;

    // Use this for initialization
    private void Start()
    {

        // Assign Renderer component
        rend = GetComponent<SpriteRenderer>();

        // Load dice sides sprites to array from DiceSides subfolder of Resources folder
        diceSides = Resources.LoadAll<Sprite>("ColorSides/");
    }

    // If you left click over the dice then RollTheDice coroutine is started
    private void OnMouseDown()
    {
        StartCoroutine("RollTheDice");
    }

    // Coroutine that rolls the dice
    private IEnumerator RollTheDice()
    {
        // Variable to contain random dice side number.
        // It needs to be assigned. Let it be 0 initially
        int randomDiceSide = 6;

        // Final side or value that dice reads in the end of coroutine
        int finalSide = 0;

        // Loop to switch dice sides ramdomly
        // before final side appears. 20 itterations here.
        for (int i = 0; i <= 20; i++)
        {
            // Pick up random value from 0 to 5 (All inclusive)
            randomDiceSide = Random.Range(0, 6);

            // Set sprite to upper face of dice from array according to random value
            rend.sprite = diceSides[randomDiceSide];

            // Pause before next itteration
            yield return new WaitForSeconds(0.05f);
        }

        // Assigning final side so you can use this value later in your game
        // for player movement for example
        finalSide = randomDiceSide + 1;

        // Show final dice value in Console
        Debug.Log(finalSide);

        if (finalSide == 1)
        {
            Debug.Log("Take a black piece");
        }
        if (finalSide == 2)
        {
            Debug.Log("Take a blue piece");
        }
        if (finalSide == 3)
        {
         Debug.Log("Take a green piece");
        }
        if (finalSide == 4)
        {
            Debug.Log("Take a red piece");
        }
        if (finalSide == 5)
        {
            Debug.Log("Take any piece");
        }
        if (finalSide == 6)
        {
            Debug.Log("Take a yellow piece");
        }
       }
}

I probably didn’t format this properly so apologies for that. I just copied this script from a dice tutorial. I did all the if(finalSide ==1) coding. So I want to replace the Debug.Log code with what it’s supposed to do. In this case allow which pieces the player can pick. In terms of the game as soon as they clicked the space the color piece would automatically fill in. In the case of the wild “take any piece” they would first have to select the color from the pieces buttons and choose the space.

I’m mostly asking for help to where I can go to learn this very specific request. But if people want to help with actually showing the code to use that would be appreciated.

put code inside these tags
1 Like

Thanks, I fixed it.