[英]Unity3d Errors With transforming position
我的错误是
错误CS1502:“ UnityEngine.Vector3.Vector3(float,float,float)”的最佳重载方法匹配具有一些无效的参数(CS1502)(Assembly-CSharp)
错误CS1503:参数'1':无法从'UnityEngine.Random'转换为'float'(CS1503)(Assembly-CSharp)
错误CS0236:字段初始化程序无法引用非静态字段,方法或属性'ResetBadPos.r'(CS0236)(Assembly-CSharp)
码:
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;
}
}
}
我编辑后剩下的错误是
错误CS0236:字段初始化程序无法引用非静态字段,方法或属性'ResetBadPos.r'(CS0236)(Assembly-CSharp)
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)(Assembly-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);
}
前两个错误将通过更改transform.position = new Vector3(r, -30, 0);
来解决transform.position = new Vector3(r, -30, 0);
transform.position = new Vector3(rInt, -30, 0);
。 这是因为您传递的是随机对象引用,而不是它所创建的随机值。
第三个错误是由于您在字段初始化程序内创建了一个Random实例,因此您需要在Start方法内调用r = new Random()
并将字段声明保留为Random r;
。
关于您的最新更新:目前,您的代码正在使用UnityEngine.Random
类 。 但是,此函数没有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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.