I got the error "error CS1061: 'Rigidbody2D' does not contain a definition for 'linearVelocity' and no accessible extension method 'linearVelocity' accepting a first argument of type 'Rigidbody2D' could be found" from this code:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Movement : MonoBehaviour
{
private Rigidbody2D rb;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
rb.linearVelocity = new Vector2(Random.Range(-10.0f,-10.0f), Random.Range(-10.0f,-10.0f));
}
// Update is called once per frame
void Update()
{
}
}
My attempt was to make a cube that gained a random velocity at the start, and continued along that path. I expected to experience an error from anything other than linearVelocity, however apparently it does not exist.
I got the error "error CS1061: 'Rigidbody2D' does not contain a definition for 'linearVelocity' and no accessible extension method 'linearVelocity' accepting a first argument of type 'Rigidbody2D' could be found" from this code:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Movement : MonoBehaviour
{
private Rigidbody2D rb;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
rb.linearVelocity = new Vector2(Random.Range(-10.0f,-10.0f), Random.Range(-10.0f,-10.0f));
}
// Update is called once per frame
void Update()
{
}
}
My attempt was to make a cube that gained a random velocity at the start, and continued along that path. I expected to experience an error from anything other than linearVelocity, however apparently it does not exist.
Share Improve this question asked Mar 17 at 11:55 NathanNathan 31 silver badge1 bronze badge 1- In general consult the API first .. select the version you use and see what exists in which Unity version – derHugo Commented Mar 17 at 18:18
1 Answer
Reset to default 2The linearVelocity
property was introduced in Unity 6, if you are using an old version, use velocity
instead:
#if UNITY_6000_0_OR_NEWER
rb.linearVelocity = ...
#else
rb.velocity = ...
#endif