0

Can some help my why does this code not executing I get the 2 debug messages from the CallRollDice function but nothing from the RollDice function.

public void CallRollDice() 
    {
        Debug.Log("clicked");
        StartCoroutine("RollDice");
        Debug.Log("ran");
    }
    private IEnumerable RollDice()
    {
        Debug.Log("called");
        
        int randomInt1;

        for (int i = 0; i < 10; i++)
        {
            randomInt1 = Random.Range(0, diceFaces.Length);
            diceImage.sprite = diceFaces[randomInt1];
            yield return new WaitForSeconds(0.05f);
        }
    }

When i researched the topic i found out that StartCoroutine function in some cases requires a function but in my case it is requires a string.

I am using unity version 2021.3.30f1 (lst)

1
  • @OP if additionally you would use the direct call StartCoroutine(RollDice()) instead of going string/name based the compiler would tell you the issue right away ;) Commented Nov 14, 2023 at 16:12

2 Answers 2

3

The documentation says:

A coroutine is a method that you declare with an IEnumerator return type and with a yield return statement included somewhere in the body.

So you should change the return type to IEnumerator.

private IEnumerator RollDice()
Sign up to request clarification or add additional context in comments.

Comments

1

IEnumerator instead of IEnumerable:

private IEnumerator RollDice()

Although strings can cause a run-time error if you typo in them. So you get the error at compile-time, I recommend:

StartCoroutine(RollDice());

// or

IEnumerator coroutine = RollDice();
StartCoroutine(coroutine);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.