Unity C# How to disable 2 scripts from another?

Hey guys so I been trying to do this for a bit now, I’ve been experimenting and I can’t figure it out. Ok so basically I’m making a horror game and when my player steps on the plane after collecting this key, its supposed pause. The way I want to do this is disabling the “MouseLook” Script on the fps controller, and disabling the “FPS Input controller”. How can I do this? I’ve tried multiple things but can’t get it down. Also I tried the yield waitforseconds thing so after the script is disabled it waits a couple of seconds and displays a message down the bottom but can’t get it to work.

Here is my latest example

using UnityEngine;
using System.Collections;
public class CollisionWithPlane : MonoBehaviour
{
   

    void OnTriggerExit(Collider coll)
   {
       if (coll.GetComponent<PickUpItem>().isKeyPickedUp)
       {
           Debug.Log("true");

           transform.position = new Vector3(56f, 0.064f, 94f);
       }
   }
    void OnTriggerEnter(Collider col)
    {
        if (col.GetComponent<PickUpItem>() != null && col.GetComponent<PickUpItem>().isKeyPickedUp)
        {
            Debug.Log("Pause");
            MouseLook instanceM = new MouseLook();
            instanceM.enabled = false;
        }
    }
}

Down here on the Debug.log(“Pause”); is where it doesn’t work.

MouseLook instanceM = new MouseLook();
InstanceM.enabled = false

You have two problems with the above code: a) you are creating a new MouseLook and disabling that instead of the existing one and b) you should never create MonoBehaviours using “new”, instead, you add them to game objects like so:

MouseLook mouseLook = aGameObject.AddComponent<MouseLook>();

The quick and dirty solution is typically to use a singleton:

public class MouseLook : MonoBehaviour
{
    public static MouseLook instance;

    public void Awake()
    {
        instance = this;
    }
}

And then do this:

MouseLook.instance.enabled = false;

Of course singletons are not a good long term solution yadda yadda yadda

As for your timer, if the coroutine is on the MouseLook, I think it will not work if you disable the script, so you might want to put it somewhere else.

Ok I’ll be trying this soon. Thanks guys :slight_smile: