Skip to main content
 首页 » 编程设计

c#之从 Unity 中的脚本关闭脚本

2024年02月06日13dyllove98

我需要从 Unity 中的另一个脚本关闭一个脚本。脚本在 C# 中,我要关闭的脚本在 JS 中。两个脚本都附加到同一个对象。我收到的错误说没有启用这样的东西。有什么想法吗?以下是我的部分代码。

void Update () 
{    
    var walkScript = GameObject.FindWithTag ("Player").GetComponent ("CharacterMotor"); 
    if ((Input.GetKey ("p")) && (stoppedMovement == true)) { 
        stoppedMovement = false; 
        walkScript.enabled = true; 
    } else if ((Input.GetKey ("p")) && (stoppedMovement == false)) { 
        stoppedMovement = true; 
        walkScript.enabled = false; 
    } 

我收到的错误如下:

Assets/Standard Assets/Character Controllers/Sources/Scripts/MouseLook.cs(41,36): error CS1061: Type UnityEngine.Component' does not contain a definition forenabled' and no extension method enabled' of typeUnityEngine.Component' could be found (are you missing a using directive or an assembly reference?)

请您参考如下方法:

Component 类没有启用属性。尝试

Behaviour walkScript = (Behaviour)GameObject.FindWithTag ("Player").GetComponent ("CharacterMotor"); 

假设这是一种行为。