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.