- Home /
Question by
mQe · Jul 03, 2012 at 08:41 PM ·
cameragetcomponent
Getting Script from Main Camera
I have a camera script that I've attached to my main camera and a player script that is trying to get hold of that camera script.
From what I've read here and there, this should work :
Cam = Camera.main.gameObject.GetComponent<SC_Camera>();
Except this returns null and yes the main camera does have the SC_Camera script. Any ideas? :-S
Comment
Answer by asimov · Jul 03, 2012 at 09:31 PM
Try:
Camera.mainCamera.GetComponent<SC_Camera>();
or
alternatively you could find the Main Camera in the scene by using:
GameObject Cam = GameObject.Find("Main Camera");
then access the script using the Cam object - this isn't required, however.
Cam.GetComponent<SC_Camera>();
GameObject.Find is a static method. Don't disguise that with the lowercase g. Camera.main is a much better approach than searching for a name.
Your answer
![](https://koobas.hobune.stream/wayback/20220613073639im_/https://answers.unity.com/themes/thub/images/avi.jpg)