【问题标题】:Turning off a script from a script in Unity从 Unity 中的脚本关闭脚本
【发布时间】:2015-02-12 13:30:21
【问题描述】:

我需要从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' 并且找不到扩展方法 enabled' of typeUnityEngine.Component'(你是缺少 using 指令或程序集引用?)

【问题讨论】:

    标签: c# unity3d unityscript gameobject unity-components


    【解决方案1】:

    类组件没有启用属性。试试

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

    假设它是一种行为。

    【讨论】:

    • 仍然出现错误:Assets/Standard Assets/Character Controllers/Sources/Scripts/MouseLook.cs(38,17):错误 CS0246:找不到类型或命名空间名称“行为”。您是否缺少 using 指令或程序集引用?
    • 这很奇怪。尝试改用“UnityEngine.Behaviour”。但它与 GameObject 在同一个命名空间中,它似乎找到了那个?也许错误消息是错误的,并且还有其他问题。如果 GameObject 可用 Behavior 也应该可用。您将他们的展位包含在“使用 UnityEngine”中;
    • 现在我得到了这个:Assets/Standard Assets/Character Controllers/Sources/Scripts/MouseLook.cs(38,39):错误 CS0266:无法隐式转换类型 UnityEngine.Component' to UnityEngine.Behaviour'。存在显式转换(您是否缺少演员表?)
    • 如何在 Unity 中将组件转换为行为?
    • 这只是基本的面向对象编程。 Component walkScript = GameObject.FindWithTag ("Player").GetComponent ("CharacterMotor"); //返回一个组件但是一个组件不能被激活 //a Behaviour can be activated and Behaviour inherits from Component so you can cast Component to Behaviour using this: Behaviour bh = (Behaviour)walkScript;
    猜你喜欢
    • 1970-01-01
    • 2016-09-22
    • 1970-01-01
    • 2012-12-17
    • 1970-01-01
    • 2012-11-12
    • 1970-01-01
    • 2011-07-30
    • 1970-01-01
    相关资源
    最近更新 更多