Unity3d 转换位置错误

Unity3d Errors With transforming position

我的错误是

Error CS1502: The best overloaded method match for 'UnityEngine.Vector3.Vector3(float, float, float)' has some invalid arguments (CS1502) (Assembly-CSharp)

Error CS1503: Argument '1': cannot convert from 'UnityEngine.Random' to 'float' (CS1503) (Assembly-CSharp)

Error CS0236: A field initializer cannot reference the non-static field, method, or property '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;
        }
    }
}

我现在编辑后留下的错误是

Error CS0236: A field initializer cannot reference the non-static field, method, or property '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' 的定义,并且找不到接受类型 'UnityEngine.Random' 的第一个参数的扩展方法 'Next'(您是否缺少 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(rInt, -30, 0); 来修复。这是因为您传递的是随机对象引用,而不是它创建的随机值。

第三个错误是由于您在字段初始化程序中创建了一个 Random 实例,因此您需要在 Start 方法中调用 r = new Random() 并将字段声明保留为 Random r;.

关于您的最新更新:目前,您的代码正在使用 UnityEngine.Random class. This one does not have a Next(int, int) function, however. It seems like you want to use the .net class System.Random which does have a Next-function.

那么您的修复将是:

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);
}