Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

How to have numbers correspond to which images are displayed?

Discussion in 'Scripting' started by frankienglish0416, Nov 27, 2019.

  1. frankienglish0416

    frankienglish0416

    Joined:
    Nov 25, 2019
    Posts:
    7
    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:
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6. using Random = UnityEngine.Random;
    7.  
    8. public class GenerateDice : MonoBehaviour
    9. {
    10.  
    11.     public GameObject TextBox0;
    12.     public GameObject TextBox1;
    13.     public GameObject TextBox2;
    14.     public GameObject TextBox3;
    15.     public GameObject TextBox4;
    16.     public GameObject TextBox5;
    17.     public int n0;
    18.     public int n1;
    19.     public int n2;
    20.     public int n3;
    21.     public int n4;
    22.     public int n5;
    23.     public int count = 0;
    24.     public string choices;
    25.     public GameObject[] textboxes = new GameObject[6];
    26.     public int kept;
    27.  
    28.  
    29.     public void Roll()
    30.     {
    31.         choices = GetInput.text;
    32.         Debug.Log(choices);
    33.         Debug.Log(choices.Length);
    34.         Debug.Log(count);
    35.         char[] charArr = choices.ToCharArray();
    36.         int[] status = Array.ConvertAll(charArr, c => (int)Char.GetNumericValue(c));
    37.         //for (int i = 0; i < status.Length; i++)
    38.         //{
    39.         //    Debug.Log(status[i]);
    40.         //}
    41.         //Debug.Log("all choices shown");
    42.         textboxes[0] = TextBox0;
    43.         textboxes[1] = TextBox1;
    44.         textboxes[2] = TextBox2;
    45.         textboxes[3] = TextBox3;
    46.         textboxes[4] = TextBox4;
    47.         textboxes[5] = TextBox5;
    48.  
    49.         // the player is out of re rolls
    50.         if (count >= 3)
    51.         {
    52.             Debug.Log("you have used all of your re rolls, dice resolved");
    53.         }
    54.  
    55.         // if the player still has re rolls left
    56.         else
    57.         {
    58.            
    59.             if (choices.Length > 0) // if status has any contents then they chose items to re roll
    60.             {
    61.                 kept = 1;
    62.             }
    63.  
    64.             if (kept == 1) // if they chose to re roll
    65.             {
    66.                 for (int i = 0; i < choices.Length; i++)
    67.                 {
    68.                     int number = Random.Range(1, 9);
    69.                     //Debug.Log(choices[i]);
    70.                     //Debug.Log(textboxes.Length);
    71.                     //Debug.Log(textboxes[choices[i]]);
    72.                     textboxes[status[i]].GetComponent<Text>().text = "" + number;
    73.                    
    74.                 }
    75.                 count++;
    76.                 kept = 0;
    77.  
    78.             }
    79.  
    80.             else
    81.             {
    82.                 n0 = Random.Range(1, 9);
    83.                 n1 = Random.Range(1, 9);
    84.                 n2 = Random.Range(1, 9);
    85.                 n3 = Random.Range(1, 9);
    86.                 n4 = Random.Range(1, 9);
    87.                 n5 = Random.Range(1, 9);
    88.                 TextBox0.GetComponent<Text>().text = "" + n0;
    89.                 TextBox1.GetComponent<Text>().text = "" + n1;
    90.                 TextBox2.GetComponent<Text>().text = "" + n2;
    91.                 TextBox3.GetComponent<Text>().text = "" + n3;
    92.                 TextBox4.GetComponent<Text>().text = "" + n4;
    93.                 TextBox5.GetComponent<Text>().text = "" + n5;
    94.                 count += 1;
    95.             }
    96.  
    97.         }
    98.  
    99.     }
    100.  
    101.  
    102. }
    103.  
     

    Attached Files:

  2. RPGFabi

    RPGFabi

    Joined:
    Oct 24, 2019
    Posts:
    29
    You could add an StringArray With the Path of the image. Then Create an Image with the Path.

    Code (CSharp):
    1. int Number = 1;
    2. string[] = Eyes = {
    3. Path_1_Eye;
    4. Path_2_Eyes;
    5. Path_3_Eyes;
    6. Path_4_Eyes;
    7. Path_5_Eyes;
    8. Path_6_Eyes;
    9. };
    10.  
    11. string CurrentPath = Eyes[Number-1];
    12.  
    13. // now give Path to Image
    I think this should do it
     
  3. frankienglish0416

    frankienglish0416

    Joined:
    Nov 25, 2019
    Posts:
    7
    Sorry Im still kind of new to Unity, when you say path do you mean the file path of where the image is?
     
  4. RPGFabi

    RPGFabi

    Joined:
    Oct 24, 2019
    Posts:
    29
    Exactly

    I'm quite new as well but I think it should do the job