我的错误是
码:
using UnityEngine;
using System.Collections;
public class ResetBadPos : MonoBehaviour {
Random r = new Random();
int rInt = r.Next(0, 100); //for ints
int range = 100;
// Use this for initialization
public int points = 0;
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnCollisionEnter2D(Collision2D col) {
if (col.gameObject.name == "Bottom Boards") {
points += 1;
transform.position = new Vector3(r, -30, 0);
Score.score += points;
}
}
}
编辑后我现在剩下的错误是
using UnityEngine;
using System.Collections;
public class ResetBadPos : MonoBehaviour {
Random r = new Random();
int rInt = r.Next(0, 100); //for ints
int range = 100;
// Use this for initialization
public int points = 0;
void Start () {
r = new Random ();
}
// Update is called once per frame
void Update () {
}
void OnCollisionEnter2D(Collision2D col) {
if (col.gameObject.name == "Bottom Boards") {
points += 1;
transform.position = new Vector3(rInt, -30, 0);
Score.score += points;
}
}
}
最新的代码和错误是现在
错误CS0236:字段初始化程序无法引用非静态字段,方法或属性'KillBad.r'(CS0236)(Assembly-CSharp)
using UnityEngine;
using System.Collections;
public class KillBad : MonoBehaviour {
Random r;
int rInt = r.Next(-50, 50); //for ints
int range = 100;
// Use this for initialization
public int points = 0;
void Start () {
r = new Random ();
}
// Update is called once per frame
void Update () {
}
void OnCollisionEnter2D(Collision2D col) {
if (col.gameObject.name == "Bottom Boards") {
points += 1;
transform.position = new Vector3(rInt, -30, 0);
Score.score += points;
}
}
}
谢谢大家,但现在我又遇到了另一个错误
错误CS1061:“UnityEngine.Random”不包含“Next”的定义,并且找不到扩展方法“Next”,该扩展方法接受“UnityEngine.Random”类型的第一个参数(您是否缺少using指令或程序集引用? )(CS1061)(程序集-CSharp)
使用UnityEngine;
使用System.Collections;
public class KillBad : MonoBehaviour {
Random r;
int rInt;
int range = 100;
// Use this for initialization
public int points = 0;
void Start () {
r = new Random ();
rInt = r.Next(-50, 50);
}
最佳答案
关于您的最新更新:目前,您的代码正在使用UnityEngine.Random
class。但是,这个没有Next(int, int)
函数。似乎您想使用确实具有Next-function的.net类System.Random
。
您的解决方案将是:
using UnityEngine;
using System.Collections;
public class KillBad : MonoBehaviour {
System.Random r;
int rInt;
int range = 100;
// Use this for initialization
public int points = 0;
void Start () {
r = new System.Random ();
rInt = r.Next(-50, 50);
}