【问题标题】:Error CS0122 'Rigidbody.AddForce(Vector3)' is inaccessible due to its protection level错误 CS0122 'Rigidbody.AddForce(Vector3)' 由于其保护级别而无法访问
【发布时间】:2021-09-14 14:34:55
【问题描述】:

我是 Unity 和 C# 的新手,我想使用“AddForce”对 3D 对象(球体)施加力。我尝试在 Visual Studio 2019(使用 Unity 2020.2.0b14)中运行以下 C# 脚本:

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

public class Script14sept : MonoBehaviour {

    public float forceValue;
    private Rigidbody rb;

    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponentInParent<Rigidbody>();
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    private void FixedUpdate()
    {
        rb.AddForce(new Vector3(Input.GetAxis("Horizontal"),
                                0,
                                Input.GetAxis("Vertical")) * forceValue);
    }
}

但是,运行此代码会导致错误: 错误 CS0122 'Rigidbody.AddForce(Vector3)' 由于其保护级别而无法访问

代码有问题还是取决于其他问题(例如,使用 Unity 的 beta 版本)?

【问题讨论】:

  • 除非您想研究最新功能,否则不要使用 BETA 版本的 Unity。您拥有的版本可能仍然充满(已经解决的)已知问题和错误......您应该坚持使用最新的LTS 版本(当前为2020.3.18f1
  • 但是,这是我第一次看到...怀疑这是针对其中一个 beta 版本进行了更改,然后突然又起作用了^^奇怪的事情...

标签: c# visual-studio unity3d


【解决方案1】:

组件 rb 设置为私有。将其设置为公开。

【讨论】:

  • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-13
  • 1970-01-01
  • 2014-08-15
  • 1970-01-01
  • 2015-09-26
  • 2011-09-01
相关资源
最近更新 更多