- Home /
Randome Number for Dice
I'm working on an easy dice roll. All it is is getting a random number from the code and it will display on the screen the two numbers. I'm having trouble though. It is in C#
using UnityEngine; using System.Collections;
public class Dice : MonoBehaviour {
public int dice1;
public int dice2;
private void RandomNumber() { //Get a random number for the dice. Random RandomNumber = new Random(); dice1 = RandomNumber.Next(6); //Show Dice 1 Debug.Log(dice1); dice2 = RandomNumber.Next(6); //Show Dice 2 Debug.Log(dice2);
}
}
.Next is underlined and it wont compile. Any suggestions on how to fix this?
Answer by Statement · Mar 20, 2011 at 11:01 PM
It's because you're using unitys Random class and not the .net/monos Random class. I am not sure which you want to use but I think simplest is to use unitys:
using UnityEngine; using System.Collections;
public class Dice : MonoBehaviour { public int dice1; public int dice2;
private void RandomNumber()
{
dice1 = Random.Range(0, 7);
Debug.Log(dice1);
dice2 = Random.Range(0, 7);
Debug.Log(dice2);
}
}
Thanks for the help. It doesn't matter which one i use as long as it works.
Answer by Eric5h5 · Mar 20, 2011 at 11:53 PM
If you want to use Random from .NET, either import the System namespace, or use System.Random to differentiate it from UnityEngine.Random.
They do the same thing, well from what I can tell they do the same thing.
Actually they don't. See the Random class docs on $$anonymous$$SDN.
Your answer
Follow this Question
Related Questions
Adding GUI to this 1 Answer
Random number with exceptions? 2 Answers
Lottery system 1 Answer
Generate 8 unique random integers 2 Answers
random number generator problem 1 Answer