在 Unity 中禁用脚本时出现 CS1061 错误

时间:2021-08-01 13:31:00

标签: c# unity3d

我正在尝试禁用脚本 PlayerMove,但第 20 行似乎有问题。

<块引用>

DisableMove.cs(20,22): 错误 CS1061: 'int' 不包含 “已启用”的定义和无可访问的扩展方法“已启用” 可以找到接受类型“int”的第一个参数(你是 缺少 using 指令或程序集引用?)

我应该如何修改它?

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

public class DisableMove : MonoBehaviour
{
    int StopMove;

    // Start is called before the first frame update
    void Start()
    {
        var StopMove = GetComponent<PlayerMove>();
    }

    // Update is called once per frame
    void Update()
    {
        if(Input.GetKeyDown(KeyCode.O))
        {
            StopMove.enabled = false;
        }
    }
}

2 个答案:

答案 0 :(得分:0)

您已将 StopMove 创建为整数类型,并将其隐藏在 Start() 中。 将脚本的第 7 行和第 12 行更新如下:

PlayerMove StopMove;

// Start is called before the first frame update
void Start()
{
    StopMove = GetComponent<PlayerMove>();
}

答案 1 :(得分:0)

您已经声明了两个“StopMove”变量,一个在类 (int) 中,另一个在 Start() (var) 方法中。

当您在 Update() 中调用“StopMove”时,脚本会找到变量 (int)。它找不到变量 (var),因为它是在 Start() 方法中创建的,无法在其他方法中访问。

我会像这样设置脚本:

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

public class DisableMove : MonoBehaviour
{
    PlayerMove StopMove;

    // Start is called before the first frame update
    void Start()
    {
        StopMove = GetComponent<PlayerMove>();
    }

    // Update is called once per frame
    void Update()
    {
        if(Input.GetKeyDown(KeyCode.O))
        {
            StopMove.enabled = false;
        }
    }
}