Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in AI and Deep Learning by (50.2k points)

What I found was how to create random numbers. Great. This solution, however, was not working in other functions. To create a random number, I used

Random randomDirection = new Random();

int directionChoice = randomDirection.Next(1, 4); 

inside of a function called enemyWalk(){};

However, this caused an 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?)

This error does not appear when I take the random integer generator out of the function. Any solutions to fix this problem?

I'm hoping to use this code to make my enemy wander around when not doing anything by randomly choosing an integer that decides which direction he walks (up, left, right, or down), then using a random double generator to determine the distance it walks. However, I need a random number generated whenever enemyWalk(){}; is called.

1 Answer

0 votes
by (107k points)

minVal is inclusive and maxVal is exclusive of the returned random value when using the integer method overload. In your case it would be:

 Random.Range(1,4);

Instead of Next(1,4).

If using floats as per:

Random.Range(1.0F, 3.5F);

Both minVal and maxVal are inclusive in this case.

For more information, refer to the following link: https://docs.unity3d.com/ScriptReference/Random.Range.html

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...