I have a dice roll screen has seen below. Inside the script for this scene is a random number generator that creates these numbers. What I am trying to achieve is whenever for example “3” is generated it will display the image of a dice with side 3. How would I go about doing this if I have the necessary assets already?
Here is the code for the dice roll script:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Random = UnityEngine.Random;
public class GenerateDice : MonoBehaviour
{
public GameObject TextBox0;
public GameObject TextBox1;
public GameObject TextBox2;
public GameObject TextBox3;
public GameObject TextBox4;
public GameObject TextBox5;
public int n0;
public int n1;
public int n2;
public int n3;
public int n4;
public int n5;
public int count = 0;
public string choices;
public GameObject[] textboxes = new GameObject[6];
public int kept;
public void Roll()
{
choices = GetInput.text;
Debug.Log(choices);
Debug.Log(choices.Length);
Debug.Log(count);
char[] charArr = choices.ToCharArray();
int[] status = Array.ConvertAll(charArr, c => (int)Char.GetNumericValue(c));
//for (int i = 0; i < status.Length; i++)
//{
// Debug.Log(status[i]);
//}
//Debug.Log("all choices shown");
textboxes[0] = TextBox0;
textboxes[1] = TextBox1;
textboxes[2] = TextBox2;
textboxes[3] = TextBox3;
textboxes[4] = TextBox4;
textboxes[5] = TextBox5;
// the player is out of re rolls
if (count >= 3)
{
Debug.Log("you have used all of your re rolls, dice resolved");
}
// if the player still has re rolls left
else
{
if (choices.Length > 0) // if status has any contents then they chose items to re roll
{
kept = 1;
}
if (kept == 1) // if they chose to re roll
{
for (int i = 0; i < choices.Length; i++)
{
int number = Random.Range(1, 9);
//Debug.Log(choices[i]);
//Debug.Log(textboxes.Length);
//Debug.Log(textboxes[choices[i]]);
textboxes[status[i]].GetComponent<Text>().text = "" + number;
}
count++;
kept = 0;
}
else
{
n0 = Random.Range(1, 9);
n1 = Random.Range(1, 9);
n2 = Random.Range(1, 9);
n3 = Random.Range(1, 9);
n4 = Random.Range(1, 9);
n5 = Random.Range(1, 9);
TextBox0.GetComponent<Text>().text = "" + n0;
TextBox1.GetComponent<Text>().text = "" + n1;
TextBox2.GetComponent<Text>().text = "" + n2;
TextBox3.GetComponent<Text>().text = "" + n3;
TextBox4.GetComponent<Text>().text = "" + n4;
TextBox5.GetComponent<Text>().text = "" + n5;
count += 1;
}
}
}
}
