- Home /
Stopping camera
Why this not work? Do i something wrong?
private GameObject firstPersonControllerCamera;
private GameObject mainCamera;
void Update (){
if(enter == true){
if(Input.GetKeyDown("f")){
TalkingMenu = true;
Screen.lockCursor = false;
firstPersonControllerCamera = gameObject.Find("First Person Controller").GetComponent("MouseLook");
mainCamera = gameObject.Find("Main Camera").GetComponent("MouseLook");
firstPersonControllerCamera.enabled = false;
mainCamera.enabled = false;
}
}
}
It's make error: Static member `UnityEngine.GameObject.Find(string)` cannot be accessed with and instance reference, qualify it with a type name instead
Answer by aldonaletto · Sep 07, 2012 at 02:32 PM
You should use GameObject.Find("name"). But there are other errors: mainCamera and firstPersonControllerCamera types should be MouseLook, not GameObject. Additionally, you should assign them only once (at Start, for instance) in order to avoid possible hiccups when pressing F (Find functions are slow). Since the First Person Controller prefab has the tag "Player", is easier and faster to find it by tag. The main camera is easier: the Camera.main variable is a Camera reference that shows which's the main camera.
private MouseLook firstPersonControllerCamera; private MouseLook mainCamera;
void Start(){
// It's easier to find the FPC by its tag:
GameObject player = GameObject.FindWithTag("Player");
firstPersonControllerCamera = player.GetComponent< MouseLook>();
// No need to Find the main camera: use Camera.main instead:
mainCamera = Camera.main.GetComponent< MouseLook>();
}
void Update (){
if(enter == true){
if(Input.GetKeyDown("f")){
TalkingMenu = true;
Screen.lockCursor = false;
firstPersonControllerCamera.enabled = false;
mainCamera.enabled = false;
}
}
}
Your answer
Follow this Question
Related Questions
360 Angle Camera Rotate 2 Answers
MissingComponentException: There is no 'Camera' 0 Answers
Final Fantasy style camera 1 Answer
Movin' camera in XYZ 6 Answers
How to make Orthographic camera ignore window size changes? 1 Answer