简体   繁体   English

转换位置时出现Unity3d错​​误

[英]Unity3d Errors With transforming position

My errors are 我的错误是

Error CS1502: The best overloaded method match for 'UnityEngine.Vector3.Vector3(float, float, float)' has some invalid arguments (CS1502) (Assembly-CSharp) 错误CS1502:“ UnityEngine.Vector3.Vector3(float,float,float)”的最佳重载方法匹配具有一些无效的参数(CS1502)(Assembly-CSharp)

Error CS1503: Argument '1': cannot convert from 'UnityEngine.Random' to 'float' (CS1503) (Assembly-CSharp) 错误CS1503:参数'1':无法从'UnityEngine.Random'转换为'float'(CS1503)(Assembly-CSharp)

Error CS0236: A field initializer cannot reference the non-static field, method, or property 'ResetBadPos.r' (CS0236) (Assembly-CSharp) 错误CS0236:字段初始化程序无法引用非静态字段,方法或属性'ResetBadPos.r'(CS0236)(Assembly-CSharp)

Code: 码:

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

The errors I have left now after editing are 我编辑后剩下的错误是

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

Newest Code and error is now Error CS0236: A field initializer cannot reference the non-static field, method, or property 'KillBad.r' (CS0236) (Assembly-CSharp) 最新的代码和错误现在为错误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;
        }
    }
}

Thanks everybody but now I am having another error Error CS1061: 'UnityEngine.Random' does not contain a definition for 'Next' and no extension method 'Next' accepting a first argument of type 'UnityEngine.Random' could be found (are you missing a using directive or an assembly reference?) (CS1061) (Assembly-CSharp) 谢谢大家,但是现在我遇到另一个错误错误CS1061:'UnityEngine.Random'不包含'Next'的定义,并且找不到扩展方法'Next'接受类型为'UnityEngine.Random'的第一个参数(您是缺少using指令或程序集引用吗?)(CS1061)(Assembly-CSharp)

using UnityEngine; 使用UnityEngine; using System.Collections; 使用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);
    }

The first two errors will be fixed by changing transform.position = new Vector3(r, -30, 0); 前两个错误将通过更改transform.position = new Vector3(r, -30, 0);来解决transform.position = new Vector3(r, -30, 0); to transform.position = new Vector3(rInt, -30, 0); transform.position = new Vector3(rInt, -30, 0); . This is because you are passing the random object reference as opposed to a random value created by it. 这是因为您传递的是随机对象引用,而不是它所创建的随机值。

The third error is down to you creating a Random instance inside the field initialiser, so you need to call r = new Random() inside the Start method and leave the field declaration as Random r; 第三个错误是由于您在字段初始化程序内创建了一个Random实例,因此您需要在Start方法内调用r = new Random()并将字段声明保留为Random r; .

Concerning your latest update: At the moment, your code is using the UnityEngine.Random class . 关于您的最新更新:目前,您的代码正在使用UnityEngine.Random This one does not have a Next(int, int) function, however. 但是,此函数没有Next(int, int)函数。 It seems like you want to use the .net class System.Random which does have a Next-function . 似乎您想使用确实具有Next-function的.net类System.Random

Your fix would then be: 您的解决方法将是:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM