如何在1个gameObject中启用/禁用脚本?

时间:2015-06-17 10:39:22

标签: c# unity3d camera switching unity5

我有gameObject MainCamera,并且脚本LookAtCamera作为我的主视图选项,我有脚本MouseLook,我想通过右键单击创建辅助。

using UnityEngine;
using System.Collections;

public class CameraManager : MonoBehaviour {

    void Update () {

        if (Input.GetKey (KeyCode.Mouse1)) {


            LookAtCamera.enabled = false;
            MouseLook.enabled = true;
        }    
    }
}

如何将脚本声明为MainCamera的公共组件?我只启用了其中一个,并在鼠标右键单击之间切换。

2 个答案:

答案 0 :(得分:0)

你已经完成了大部分工作。因此,将脚本声明为公共变量,如下所示,然后在检查器上分配它们:

using UnityEngine;
using System.Collections;

public class CameraManager : MonoBehaviour {

    public LookAtCamera lookAtScript;
    public MouseLook mouseLookScript;

    void Update () {
        if (Input.GetKey (KeyCode.Mouse1)) {
            lookAtScript.enabled = false;
            mouseLookScript.enabled = true;
        }    
    }
}

答案 1 :(得分:0)

Thx :)我不知道如何声明脚本。我还添加了其他回到默认摄像头设置。

 using UnityEngine;
using System.Collections;

public class CameraManager : MonoBehaviour {

    public LookAtCamera lookAtScript;
    public MouseLook mouseLookScript;

    void Update () {
        if (Input.GetKey (KeyCode.Mouse1)) {
            lookAtScript.enabled = false;
            mouseLookScript.enabled = true;
        } else {
            mouseLookScript.enabled = false;
            lookAtScript.enabled = true;

        }
    }
}