简体   繁体   中英

How to enable/disable scripts in 1 gameObject?

I have gameObject MainCamera and there is script LookAtCamera as my main view option and I have script MouseLook that I wanna make secondary with right-click.

using UnityEngine;
using System.Collections;

public class CameraManager : MonoBehaviour {

    void Update () {

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


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

How to declare script as a public component of MainCamera? I just have one of them enabled and switch between with mouse right click.

You've got most of it done. So, declare the scripts as a public variable like below, and then assign them on the inspector:

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;
        }    
    }
}

Thx :) I didn't know how to declare script. I also added else to go back to default camera setup.

 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;

        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM