- Home /
().enabled=false stopped working
Hi,I upon creating pause/console button of sorts in c#,I used get component to disable & enable mouse look (both vertical[in main camera] & horizontal[in fps controller]). This worked fine for couple of days, but after downloading free asset from asset store(had 1 JS script) now my own script gives me a null reference on those lines(inside update).
if(CheatMenuOpen==true)
{
fpsContrl.GetComponent<MouseLook>().enabled=false;
mcamera.GetComponent<MouseLook>().enabled=false;
}
else
{
fpsContrl.GetComponent<MouseLook>().enabled=true;
mcamera.GetComponent<MouseLook>().enabled=true;
}
fpsContrl & mcamera are public. I have battled with this null reference exception whole morning and I just dont know how to fix it. Any help is appreciated, thanks in advance :|
When that still worked, it "ticked" them to disabled and back(in inspector) when told to.
Answer by Jessespike · Jul 20, 2015 at 04:54 PM
GetComponent needs a type to be specified. Presuming the components are already being referenced in the script, and fpsContrl is of type "FirstPersonController" and mcamera being a "Camera" type:
if (CheatMenuOpen == true) {
if (fpsContrl!= null) fpsContrl.enabled = false;
if (mcamera != null) mcamera.enabled = false;
} else {
if (fpsContrl!= null) fpsContrl.enabled = true;
if (mcamera != null) mcamera.enabled = true;
}
If you want to get a component from a GameObject:
fpsContrl = (FirstPersonController) someGameObject.GetComponent<FirstPersonController>();
mcamera = (Camera) someGameObject.GetComponent<Camera>();
http://docs.unity3d.com/ScriptReference/GameObject.GetComponent.html
O god I apologize, code I show here is missing $$anonymous$$ouseLook all together, first post updated. I referred to the game objects with just setting them public & drag&dropping them to the inspector slots.
If you use the generic version of GetComponent you don't need the cast, the method already returns an object of the correct type.
fpsContrl = someGameObject.GetComponent<FirstPersonController>();
mcamera = someGameObject.GetComponent<Camera>();
Answer by nisovin · Jul 20, 2015 at 08:13 PM
If you're getting a null reference in that block of code, then one of the following must be true:
fpsContrl is null (not initialized or not set in the inspector)
mcamera is null (not initialized or not set in the inspector)
fpsContrl doesn't have a MouseLook component
mcamera doesn't have a MouseLook component
I'd add some Debug.Log lines to figure out which thing is null. Perhaps something like:
Debug.Log(fpsContrl);
Debug.Log(mcamera);
Debug.Log(fpsContrl.GetComponent<MouseLook>());
Debug.Log(mcamera.GetComponent<MouseLook>());
Debug logs helped all right, seems that only $$anonymous$$ouseLook components return null, although the scripts are on fpsConrt & mcamera.
Hmm... it's hard to say. Are they beco$$anonymous$$g disabled or removed at runtime?
When it worked, it "ticked" to disabled and back(in inspector) when needed.
I mean the $$anonymous$$ouseLook script itself. Is it getting removed from the objects somehow?
No, $$anonymous$$ouse look script is "ticked"/disabled, but it still stayed in the fpsContrl & the camera. Tho now I cant see it due to the Null(=does nothing).
Your answer
![](https://koobas.hobune.stream/wayback/20220611232129im_/https://answers.unity.com/themes/thub/images/avi.jpg)