- Home /
MouseLook not being disabled when attached to a camera
I'm writing a script to disable moving the camera and moving the character when certain variables are true/false. It works fine, except for the script attached to the camera (I have the same problem in a similar script that handles pausing).
Here's my code:
var inUse : boolean = false;
var charControllerDisabled : boolean = false;
private var FirstPersonController : GameObject;
function Start() {
FirstPersonController = GameObject.Find("FirstPersonController");
}
function Update () {
if(inUse && !charControllerDisabled){
FirstPersonController.GetComponent("CharacterController").enabled = false;
FirstPersonController.GetComponent("MouseLook").enabled = false;
FirstPersonController.GetComponent("CharacterMotor").enabled = false;
FirstPersonController.GetComponent("FPSInputController").enabled = false;
FirstPersonController.GetComponentInChildren(MouseLook).enabled = false;
}
}
I suspect the problem is that the camera is not a GameObject but a Camera. However, I've tried referencing the Camera directly, and that didn't work either. Any help would be appreciated.
Just a comment: pulling "GetComponet" each frame (through Update that runs 60 fps) is RATHER BAD for performance. You should move those calls into a seperate function and ONLY call it when something changes.
IF you need to access them each frame, then consider to store the result (the Component pointer object) in a class-local variable and then use .enable on that object.
Thanks a lot! I'll make sure to change that in my code. Always forget that I should initialize stuff in the variables :-P
Answer by Clunk · Sep 16, 2011 at 05:16 PM
Here is what I tried, worked for me :)
Camera.mainCamera.GetComponent("MouseLook").enabled = false;
Works perfectly, thanks! I haven't had much experience with the Camera object, so this syntax is fairly new to me: thanks for helping me out :D
Your answer
Follow this Question
Related Questions
Script to disable MouseLook? 2 Answers
Turn Off/on culling mask by script? 2 Answers
Stop Motion Camera Effect 0 Answers