Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

CoRoutine Assistance Needed Please

Discussion in 'Getting Started' started by DocJ, Aug 4, 2017.

  1. DocJ

    DocJ

    Joined:
    Dec 9, 2016
    Posts:
    98
    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.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class RandomDice : MonoBehaviour {
    7.  
    8.     public Sprite[] DiceHolder;
    9.     public Image Die1;
    10.     public Image Die2;
    11.     public Image Die3;
    12.     int redDice1ChosenFromTheArray, redDice2ChosenFromTheArray, redDice3ChosenFromTheArray;
    13.     public int totalRolledAmount;
    14.     public GameObject BoxToShowNumberRolled; // Empty GameObject to display Total of 2 Dice Rolled
    15.  
    16.  
    17.     void Start ()
    18.     {
    19.         StartCoroutine (PickRandomDice ());
    20.     }
    21.  
    22.     IEnumerator PickRandomDice ()
    23.         {
    24.          
    25.         int die1Repeater = (Random.Range (3, 7));
    26.         for (int i = 0; i < die1Repeater; i++)
    27.         {
    28.             redDice1ChosenFromTheArray = Random.Range (0, DiceHolder.Length);
    29.             Die1.sprite = DiceHolder [redDice1ChosenFromTheArray];
    30.             yield return new WaitForSeconds (0.2f);
    31.             Debug.Log (redDice1ChosenFromTheArray + 1);
    32.         }
    33.  
    34.         int die2Repeater = (Random.Range (3, 7));
    35.         for (int i = 0; i < die2Repeater; i++)
    36.         {
    37.             redDice2ChosenFromTheArray = Random.Range (0, DiceHolder.Length);
    38.             Die2.sprite = DiceHolder [redDice2ChosenFromTheArray];
    39.             yield return new WaitForSeconds (0.3f);
    40.             Debug.Log (redDice2ChosenFromTheArray + 1);
    41.         }
    42.  
    43.         int die3Repeater = (Random.Range (3, 7));
    44.         for (int i = 0; i < die3Repeater; i++)
    45.         {
    46.             redDice3ChosenFromTheArray = Random.Range (0, DiceHolder.Length);
    47.             Die3.sprite = DiceHolder [redDice3ChosenFromTheArray];
    48.             yield return new WaitForSeconds (0.4f);
    49.             Debug.Log (redDice3ChosenFromTheArray + 1);
    50.         }
    51.  
    52.             totalRolledAmount = (redDice1ChosenFromTheArray + redDice2ChosenFromTheArray + redDice3ChosenFromTheArray + 3);
    53.             BoxToShowNumberRolled.GetComponent<Text> ().text = "" + totalRolledAmount; // Box next to Roll button displays total of 2 dice
    54.             Debug.Log ("The total of the 3 dice = " + totalRolledAmount); // Displays the Total of the Dice in Console
    55.         }
    56.      
    57.     }
    58.  
     
  2. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    Your thinking is definitely correct. Instead of being in start, the invocation of the coroutine should be within another method that you call as needed, not on Start.

    There's a GameObject slot on the button's OnClick section. Make sure you drag the object there that has this script attached. Then any public methods that take no parameters (and I believe methods that take a single string or number parameter) will show up in the list of functions under the dropdown.
     
    DocJ likes this.
  3. DocJ

    DocJ

    Joined:
    Dec 9, 2016
    Posts:
    98
    Is the occasional synonym for Newbie......DumbA**?

    I figured out my own problem......finally. I was adding the RollButton to the Button Script On Click, instead of the gameObject with the actual RandomDice script attached to it. :confused::rolleyes:
     
  4. DocJ

    DocJ

    Joined:
    Dec 9, 2016
    Posts:
    98
    I must have literally been typing the above as you replied. You are spot on correct, as I found out on my own seconds before reading your post. Thank you so much for the guidance though. Always appreciated!!
     
    Schneider21 likes this.
  5. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    It happens. I start way more help request threads than I ever post here. I'm such a long-winded typer that by the time I've finished writing my post, I've usually thought of an idea or two to chase down first.

    In programming, it's known as Rubber Ducking. The idea being that rubber ducks know nothing about programming. So when you run into a problem, you explain it to the duck in a way they'd understand. Doing so usually forces you to think about the problem in a new way, which often reveals a solution, or at least, a hint towards the solution. It's super effective, and is why I keep a rubber duck at my desk both at work and at home.