【发布时间】:2016-05-08 23:22:30
【问题描述】:
我已经使用 C++ 很长时间了。现在,我刚刚开始在 Unity3D 中使用 C#。我几乎完成了一个应用程序,但我似乎无法在 Unity C# 中找到一个有效的 int 生成器(具有最小值和最大值)。 我尝试了以下方法:
r = Random.Range(min, max)
和
Random rand = new Random();
r = rand.next(min, max)
两者都不起作用。我知道你在 C++ 中使用:
r = rand() % max + min;
如果你想要一个随机数。但是 Unity C# 是什么情况? 代码:
编辑:
using UnityEngine;
using System.Collections;
public class STarget : MonoBehaviour {
public GameObject inter;
int rand;
public GameObject TS1;
public GameObject TS2;
public GameObject TS3;
public GameObject TS4;
public GameObject TS5;
void Update () {
rand = Random.Range(1, 6);
}
public void GameRun () {
switch (rand){
case 1:
inter = TS1;
break;
case 2:
inter = TS2;
break;
case 3:
inter = TS3;
break;
case 4:
inter = TS4;
break;
case 5:
inter = TS5;
break;
}
Game.TargetSingle(inter);
}
}
【问题讨论】:
-
解释“没用”,你有例外吗?你得到了你意想不到的结果?如果您不描述问题,我们将无法帮助您解决问题。
-
rand.Next(min,max)结帐。确保你没有落入the hole of new-ing identically seeded randoms in a loop -
Random.Range(min,max)也应该可以工作。取决于你想要什么。 -
rand.Next 在您使用 Unity 时不会检出。请记住,我说的不是普通的 C#。
-
你的剧本是TOTALLY BIZARRE。它甚至没有显示您调用 GameRun 的位置。您对 Update() 的使用非常奇怪。