0

I am adding onto the FirstPersonController script in my game to make it such that when the player collides with water (which I gave the "water" tag) time would stop and MouseLook would be disabled. I was able to get it to identify water and set the time scale to 0; but I can't figure out how to disable MouseLook.

When I tried to use this:

GetComponent<MouseLook>().enabled = false;

I get the error:

'MouseLook' does not contain a definition for 'enabled' and no accessible extension method 'enabled' accepting a first argument of type 'MouseLook' could be found (are you missing a using directive or an assembly reference?)

So, I looked up how to remove the error, and changed it to this:

(gameObject.GetComponent("MouseLook") as MonoBehaviour).enabled = false;

But, upon collision with the water, that does nothing, and instead gives the error:

NullReferenceException: Object reference not set to an instance of an object

For those who want to see everything I added, this is the only change I made:

void OnTriggerEnter(Collider target)
    {
        if (target.gameObject.tag == "Water")
        { 
            Time.timeScale = 0.0f;
            Time.fixedDeltaTime = 0.0f;

            (gameObject.GetComponent("MouseLook") as MonoBehaviour).enabled = false;
        }
    }

Edit: This is not a duplicate of What is a NullReferenceException, and how do I fix it?; I understand the meaning of a NullReferenceException. I simply want to know how to use GetComponent on another script, then disable it.

  • 1
    What is `MouseLock`? Is it a class that you write? If you want to use `enabled`, it should inherit `MonoBehavior`. And did you check if the `MouseLock` is added to gameobject? – Heejae Kim Feb 25 '19 at 03:20
  • If `MouseLock` does not inherit from `MonoBehaviour` than trying to typecast it via `as` will return `null` -> NullReferenceException – derHugo Feb 25 '19 at 04:22
  • MouseLook is another script in FirstPersonController. – Asher Drummond Feb 25 '19 at 13:51
  • Why don't you change it to this: (gameObject.GetComponent() as MonoBehaviour).enabled. Obviously GetComponent returns null, when you call it with a string parameter. – slfan Feb 25 '19 at 18:36
  • I had tried that. It gave me a "Cannot convert type" error. I'm looking into solving the problem without GetComponent. – Asher Drummond Feb 25 '19 at 21:26

1 Answers1

0

you may try this.drag yoyr mouse look game object in to public field in the inspector

public MouseLock mouseLook;

    void OnTriggerEnter(Collider target)
        {
            if (target.gameObject.tag == "Water")
            { 
                Time.timeScale = 0.0f;
                Time.fixedDeltaTime = 0.0f;

                mouseLook.enabled = false;
            }
        }
Thisara
  • 644
  • 3
  • 11
  • 22
  • This returns the error `:'MouseLook' does not contain a definition for 'enabled' and no accessible extension method 'enabled' accepting a first argument of type 'MouseLook' could be found (are you missing a using directive or an assembly reference?)` – Asher Drummond Feb 25 '19 at 14:02