Hi everyone,
I’ve been trying to enhance the visual of the “Dice Roll” on my Main Scene. Basically, it’s a 2D “Dice Roll”. I have 3 Sprites that will randomly display a number on the empty image holder, making it look like 3 dice were “rolled”/“chosen”.
Instead of having it coded where it just picks a number randomly and displays it, I added more code so that it randomly generates a number of times for each die. Unfortunately when it ran though, it displayed the randomly chosen numbers as if they were only chosen once. It didn’t look like it went through a few numbers before “landing” on one final number. I think the frame rate was too fast. So I added a StartCoRoutine because I could then add a WaitForSeconds in it. That worked perfectly. However, it created a problem for me, which I feel I’m overlooking something through all my research (the trouble with being new is the fact it might be staring me in the face and I wouldn’t see it).
My problem is; I have it under Start () and it works as soon as I hit the play button on Unity. That’s understandable. However, I would like to be able to have it wait until I click the actual Roll Button on the screen. I tried changing the Start () (Line 17) to public void RollButtonClicked (), but that’s where I run into different trouble. RollButtonClicked () doesn’t appear under my Function list in the Unity Editor Button On Click drop down menu.
Thank you, as always, for any guidance on this issue.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class RandomDice : MonoBehaviour {
public Sprite[] DiceHolder;
public Image Die1;
public Image Die2;
public Image Die3;
int redDice1ChosenFromTheArray, redDice2ChosenFromTheArray, redDice3ChosenFromTheArray;
public int totalRolledAmount;
public GameObject BoxToShowNumberRolled; // Empty GameObject to display Total of 2 Dice Rolled
void Start ()
{
StartCoroutine (PickRandomDice ());
}
IEnumerator PickRandomDice ()
{
int die1Repeater = (Random.Range (3, 7));
for (int i = 0; i < die1Repeater; i++)
{
redDice1ChosenFromTheArray = Random.Range (0, DiceHolder.Length);
Die1.sprite = DiceHolder [redDice1ChosenFromTheArray];
yield return new WaitForSeconds (0.2f);
Debug.Log (redDice1ChosenFromTheArray + 1);
}
int die2Repeater = (Random.Range (3, 7));
for (int i = 0; i < die2Repeater; i++)
{
redDice2ChosenFromTheArray = Random.Range (0, DiceHolder.Length);
Die2.sprite = DiceHolder [redDice2ChosenFromTheArray];
yield return new WaitForSeconds (0.3f);
Debug.Log (redDice2ChosenFromTheArray + 1);
}
int die3Repeater = (Random.Range (3, 7));
for (int i = 0; i < die3Repeater; i++)
{
redDice3ChosenFromTheArray = Random.Range (0, DiceHolder.Length);
Die3.sprite = DiceHolder [redDice3ChosenFromTheArray];
yield return new WaitForSeconds (0.4f);
Debug.Log (redDice3ChosenFromTheArray + 1);
}
totalRolledAmount = (redDice1ChosenFromTheArray + redDice2ChosenFromTheArray + redDice3ChosenFromTheArray + 3);
BoxToShowNumberRolled.GetComponent<Text> ().text = "" + totalRolledAmount; // Box next to Roll button displays total of 2 dice
Debug.Log ("The total of the 3 dice = " + totalRolledAmount); // Displays the Total of the Dice in Console
}
}