How to enable/disable scripts in 1 gameObject?

281 views Asked by At

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.

2

There are 2 answers

0
Phantoms On

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;

        }
    }
}
0
Philip Rowlands On

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