简体   繁体   English

我正在尝试为我的游戏制作一个随机数生成器并为其分配一个整数,以便我以后可以使用它但给我一个错误

[英]I'm trying to make a random number generator for my game and assign a integer to it so i can use it later but give me a error

Here's my code:这是我的代码:

public class Random
{
    public void pipeSpawn()
    {
        Random rnd = new Random();

        int num = rnd.Next(1, 9);
    }
}

I get an error underlined under Next saying my method doesn't contain a definition for Next?我在 Next 下得到一个带下划线的错误,说我的方法不包含 Next 的定义?

Note: Sorry if my explanation is bad I'm not great at explaining / coding.注意:对不起,如果我的解释不好,我不擅长解释/编码。

Sounds like you are trying to use the Random class as defined in .NET but I suspect the include is pointing to the one in Unity .听起来您正在尝试使用 .NET 中定义的Random类,但我怀疑include指向Unity中的那个。

Unity:统一:

Random

Easily generate random data for games.轻松生成游戏的随机数据。

This static class provides several easy game-oriented ways of generating pseudorandom numbers.这个静态类提供了几种简单的面向游戏的生成伪随机数的方法。

Unity's Random has no Next method. Unity 的Random没有Next方法。 Also it is a static class so you don't new it.它也是一个static类,所以你不要new它。

In your case just use Unity's:在您的情况下,只需使用 Unity 的:

int num = (int) Random.Range(1f, 9f);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在尝试制作一个简单的 2d 平台游戏,但我的代码不会让我跳 - I'm trying to make a simple 2d platformer game, but my code wont let me jump 我正在努力使文本框中的数字无法增加 - I'm trying to make it so that the number cant be increased in a TextBox 我正在努力使玩游戏的人可以在触发器中按他们想要的次数按“E” | Unity 3d + 动画师 - I'm trying to make it so the person playing the game can press "E" as much as they want in the trigger | Unity 3d + animator 我正在尝试为 D&D 循环随机数生成器,但我的代码不起作用 - I am trying to make a do while looping random number generator for D&D, but my code doesn't work 我正在尝试统一构建游戏,但这给了我一个错误 - I am trying to build my game in unity but it is giving me an error 我想在 Xamarin 中制作一个增量游戏,并且我制作了一些给我一个异常错误的计时器 - I want to make a incremental game in Xamarin, and i made some timers that give me an exception error 我正在努力让我的 Unity 游戏在游戏结束后重新启动 - I am trying to make it so that my Unity game restarts after the game has ended 如何使克隆的游戏对象成为全局对象,以便可以在Unity功能之外使用它们? - How do I make my cloned game objects global, so I can use them outside of the function in Unity? 每次调用构造函数时,如何使C#随机数生成器更改? - How do I make my C# random number generator change with each time the constructor is called? 我每次都尝试创建随机地图,但我没有得到想要的结果,我该如何在每个地图中创建随机路径? - I'm trying to create random map each time but i'm not getting the result i wanted how can i make a random path in each map?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM