Lots of tutorials on how to make a dice script. Very few on how to use dice results to do something.
I’m wondering how in this script I get the RollTheDice method to “publish” the finalSide result so that I can use it? Or how do I get a method to look inside RollTheDice and find finalSide? I’ve been stuck on this for days now. Trying different things. I’m too new to programming to really understand.
using System.Collections;
using UnityEngine;
public class Dice : MonoBehaviour {
private Sprite[] diceSides;
private SpriteRenderer rend;
private void Start () {
rend = GetComponent<SpriteRenderer>();
diceSides = Resources.LoadAll<Sprite>("DiceSides/");
}
private void OnMouseDown()
{
StartCoroutine("RollTheDice");
}
// Coroutine that rolls the dice
private IEnumerator RollTheDice()
{
int randomDiceSide = 0;
int finalSide = 0;
for (int i = 0; i <= 20; i++)
{
randomDiceSide = Random.Range(0, 5);
rend.sprite = diceSides[randomDiceSide];
yield return new WaitForSeconds(0.05f);
}
finalSide = randomDiceSide + 1;
Debug.Log(finalSide);
}
}