最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Why did I get the unity error "error CS1061: 'Rigidbody2D' does not contain a definition for &

programmeradmin4浏览0评论

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
Add a comment  | 

1 Answer 1

Reset to default 2

The 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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论