x


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?

more ▼

asked Mar 20 '11 at 10:53 PM

crzyone9584 gravatar image

crzyone9584
23 7 7 13

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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);
    }
}
more ▼

answered Mar 20 '11 at 11:01 PM

Statement gravatar image

Statement ♦♦
20.1k 35 70 175

Thanks for the help. It doesn't matter which one i use as long as it works.

Mar 20 '11 at 11:13 PM crzyone9584
(comments are locked)
10|3000 characters needed characters left

If you want to use Random from .NET, either import the System namespace, or use System.Random to differentiate it from UnityEngine.Random.

more ▼

answered Mar 20 '11 at 11:53 PM

Eric5h5 gravatar image

Eric5h5
80.1k 41 132 519

They do the same thing, well from what I can tell they do the same thing.

Mar 21 '11 at 01:25 AM crzyone9584

Actually they don't. See the Random class docs on MSDN.

Mar 21 '11 at 04:45 AM Eric5h5
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x571
x76
x18

asked: Mar 20 '11 at 10:53 PM

Seen: 1590 times

Last Updated: Mar 20 '11 at 10:53 PM