Reputation: 211
i am writing a game in unity and i want to create one random integer number... i am using the following:
public Random ran = new Random();
public int power = ran.Next(0, 10);
but when i want to run the program it gives me the following error:
Type 'UnityEngine.Random' does not contain a definition for 'Next' and no extension method 'Next' of type `UnityEngine.Random' could be found (are you missing a using directive or an assembly reference?)
Does anyone help me on what is going wrong ???
Upvotes: 21
Views: 29654
Reputation: 23
Have you tried to delete the "Using System"? Because if you write Random it does not know if you mean the Random in System or UnityEngine.
Upvotes: 0
Reputation: 2130
As others have said, Unity has its own pseudo-random number generator, which is what you are automatically picking up, and as nightRider has said the syntax for that PRNG would be var number = Random.Range(0,10)
;
It's usually floats that you want in Unity, so using Unity's own PRNG avoids the possible overhead (I've not tested it) of having to generate doubles and casting them to floats, and it also automatically gives a single PRNG for all scripts, which can simplify debugging when you want to initialise the PRNG with a seed for consistent behaviour. It also offers some other game-oriented methods and properties such as generating random points inside or on a unit sphere. On the other hand, it does not provide bit or double pseudo-random numbers, so if you want a lot of those then System.Random might be a better choice, though for repeatable behaviour when debugging you'll want to make that PRNG common to all scripts and seed it just once, which is a bit of an extra headache.
Upvotes: 0
Reputation: 1
Unity has a built in Random class. Instead you can use:
var randomNumber = Random.Range(10);
Upvotes: 0
Reputation: 53958
You should use the System.Random
class, since this class has a method called Next
. For further documentation on this please have a look here. I suppose that this is your error, since from the error message you get, it's clear that the UnityEngine.Random
class is used and not the System.Random
. In terms of code, I would try this:
public System.Random ran = new System.Random();
public int power = ran.Next(0, 10);
Update
Using the System.Random
we will solve the issue of the naming collision but another problem would be arise. Typing the above two lines inside the body of a class, like below:
public class Program
{
public System.Random ran = new System.Random();
public int power = ran.Next(0, 10);
}
you will notice that the compiler warns you that something wrong is going on and if you try to build your project, you will get the following message:
A field initializer cannot reference the non-static field, method, or property
In order to fix this, there are two options:
a) Make ran
to be static:
public class Program
{
public static System.Random ran = new System.Random();
public int power = ran.Next(0, 10);
}
b) Move the initialization of power
inside the constructor:
public class Program
{
public System.Random ran = new System.Random();
public int power;
public Program()
{
power = ran.Next(0, 10);
}
}
Upvotes: 26