CS0120非静态字段,方法或属性& quot; Rigidbody2D.velocity和警告CS0642需要对象引用 [英] CS0120 an object reference is required for the non static field, method or property "Rigidbody2D.velocity and warning CS0642

查看:80
本文介绍了CS0120非静态字段,方法或属性& quot; Rigidbody2D.velocity和警告CS0642需要对象引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编程和制作游戏的初学者{这是我第一个遇到问题的代码}请帮助 - 我使用UNITY版本2018.3.0f2个人版和我的视觉工作室版本是15.9.4

I am a beginner at programing and making games{THIS IS MY FIRST CODE THAT I AM HAVING PROBLEM WITH}Please help--I AM USING UNITY VERSION 2018.3.0f2 personal and my visual studio version is 15.9.4

使用System.Collections;

使用System.Collections.Generic;

使用UnityEngine;



公共课NewBehaviourScript:MonoBehaviour

{

    public float MoveSpeed;

   公共浮动JumpHeight;

    //在第一帧更新之前调用Start。
    void Start()

    {

        

    }


    //每帧调用一次更新为
    void Update()

    {

        if(Input.GetKeyDown(KeyCode.Space));
$
        {

           Rigidbody2D.velocity = new Vector2(0,JumpHeight);

        }
    }
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NewBehaviourScript : MonoBehaviour
{
    public float MoveSpeed;
    public float JumpHeight;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space));
        {
           Rigidbody2D.velocity = new Vector2(0, JumpHeight);
        }
    }
}

推荐答案

请使用代码编辑器发布代码论坛工具栏。另外,请向我们提供您所遇到的问题及其发生线的描述,而不是简单地将其放入标题中。

Please post code using the code editor in the forum toolbar. Also please provide us a description of the issue you're having and the line it is occurring on rather than simply putting it in the title.

您遇到的错误似乎即将到来来自Rigidbody2D.velocity。这个属性定义在哪里?您可以选择它并按F12跳转到其定义。它必须是NewBehaviourScript类中的属性或字段,否则将无法编译。

The error you're getting seems to be coming from Rigidbody2D.velocity. Where is this property defined at? You can select it and press F12 to jump to its definition. It needs to be a property or field in your NewBehaviourScript class otherwise it won't compile.

该属性的类型也需要具有名为`velocity`的属性/字段。一般来说,公众成员都是帕斯卡尔,所以这个名字是可疑的。它可能只是一个套管问题,如果属性(你F12')称之为'Velocity`就像通常是
完成。

The type of that property needs to have a property/field called `velocity` as well. In general public members are Pascal cased so the name is suspect. It might just be a casing issue if the property (that you F12'ed to) called it `Velocity` like is generally done.

这就是我期望看到的在你的代码中工作。

Here's what I would expect to see in your code for this to work.

//Whatever type Rigidbody2D is supposed to be...
public class TypeOfRigidBody2D
{
   //This should be Pascal cased and should be a property
   public Vector2 velocity { get; set; }
}

public class NewBehaviourScript : MonoBehaviour
{
   //Need to declare and initialize the property
   public TypeOfRigidBody2D Rigidbody2D { get; set; } = new TypeOfRigidBody2D();  
}

我在这里假设您正在使用支持最新C#功能的Unity版本,例如自动属性和字段初始化器。如果你不是,那么上面的代码将无法正确编译,你需要走很长的路。

I'm making an assumption here that you're using a version of Unity that supports the latest C# features such as auto properties and field initializers for them. If you aren't then the above code won't compile correctly and you'll need to go the long way.

//Whatever type Rigidbody2D is supposed to be...
public class TypeOfRigidBody2D
{
   //This should be Pascal cased and should be a property
   //This version of Unity doesn't support auto properties so do it manually
   //public Vector2 velocity { get; set; }
   public Vector2 velocity
   {
      get { return _velocity; }
      set { _velocity = value; }
   }

   private Vector2 _velocity;
}

public class NewBehaviourScript : MonoBehaviour
{
   //Need to declare and initialize the property
   //This version of Unity doesn't support auto properties or
   //field initializers on them so do it the long way
   //public TypeOfRigidBody2D Rigidbody2D { get; set; } = new TypeOfRigidBody2D();  
   public TypeOfRigidBody2D Rigidbody2D
   {
      get { return _body; }
      set { _body = value; }
   }

   private TypeOfRigidBody2D _body = new TypeOfRigidBody2D();
}


这篇关于CS0120非静态字段,方法或属性& quot; Rigidbody2D.velocity和警告CS0642需要对象引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆