Search code examples
c#error-handlingunity-game-engineposition

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)

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)

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)

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)

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)

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

Solution

  • Concerning your latest update: At the moment, your code is using the 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.

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