尝试在 C# unity 4.69 中制作随机数生成器 [英] Trying to make a random number generator in C# unity 4.69
问题描述
根据网上的许多消息来源,这正是在 Unity 4.6 中使用 C# 制作随机数生成器的方法,我什至看到一个 youtube 视频完全按照我的方式编写,但它似乎对我不起作用,错误消息说当前上下文中不存在名称‘n’".
According to many sources online, this is exactly how to make a random number generator using C# in unity 4.6, I even saw one youtube video write it exactly how I did, but it doesn't seem to work for me, the error message says "the name 'n' does not exist in the current context".
我还查看了与此类似的 C# 问题,这些问题已在 stackoverflow 上解决并尝试了相同的解决方案,但我得到的只是错误.
I've also looked at similar C# questions to this that have been solved on stackoverflow and tried the same solutions and all I get are errors.
这是我的代码:
void Start () {
n = Random.Range (1, 1000);
print (n);
}
推荐答案
必须先声明一个变量,然后才能使用它.使用前声明n
:
You must declare a variable before using it. Declare n
before using it:
int n = 0;
void Start ()
{
n = Random.Range (1, 1000);
print (n);
}
或
void Start ()
{
int n = Random.Range (1, 1000);
print (n);
}
现在,如果出现错误:
'Random' 是 'UnityEngine.Random' 和'System.Random'
'Random' is an ambiguous reference between 'UnityEngine.Random' and 'System.Random'
那是因为System
和UnityEngine
都是用using System;
和using UnityEngine
Use
That's because System
and UnityEngine
are both imported with using System;
and using UnityEngine
Use
UnityEngine.Random.Range(1, 1000);
这篇关于尝试在 C# unity 4.69 中制作随机数生成器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!