保护等级使变量无法访问

时间:2017-10-12 20:22:25

标签: c# unity3d unity5

我正在尝试使用Unity和C#制作第一人称游戏。使用我自己创建的一个脚本,我试图从库存" FirstPersonController"中访问运行速度和行走速度变量。

我已将所需的变量更改为Public,我甚至可以在属性检查器窗口中看到它们,但我自己的脚本无法访问它们,并出现以下错误:

1)无法访问变量Run和Walk速度。

Assets/PlayerVitals.cs(134,30): error CS0122: `UnityStandardAssets.Characters.FirstPerson.FirstPersonController.m_RunSpeed' is inaccessible due to its protection level

2)无法访问我添加到FirstPersonController的变量,声称它不存在(即使它显示在Unity Property Inspector中 - 附带截图)

Assets/PlayerVitals.cs(134,60): error CS1061: Type `UnityStandardAssets.Characters.FirstPerson.FirstPersonController' does not contain a definition for `m_NormRunSpeed' and no extension method `m_NormRunSpeed' of type `UnityStandardAssets.Characters.FirstPerson.FirstPersonController' could be found. Are you missing an assembly reference?

以下是两个代码的相关部分:

FirstPersonController.cs:

namespace UnityStandardAssets.Characters.FirstPerson
{
    [RequireComponent(typeof (CharacterController))]
    [RequireComponent(typeof (AudioSource))]
    public class FirstPersonController : MonoBehaviour
    {
        [SerializeField] private bool m_IsWalking;
        [SerializeField] public float m_WalkSpeed;
        [SerializeField] public float m_RunSpeed;
        [SerializeField] public float m_NormRunSpeed;

PlayerVitals.cs:

using UnityStandardAssets.Characters.FirstPerson;

public class PlayerVitals : MonoBehaviour {
public FirstPersonController playerController;
playerController = GetComponent<FirstPersonController>();
.
.
currentSpeed = playerController.m_RunSpeed;

Property Inspector

1 个答案:

答案 0 :(得分:0)

发现错误。我制作了FirstPersonController的备份副本,甚至重命名了它,但不知何故它导致了这个问题(即使它完全在不同的目录中)。删除它修复了这个问题!