- Home /
Using a second camera and finding it using a tag
I have created an camera which i want to be activated on certain instances when i go to a scene. I start off in a different scene then i change my scene which works perfectly fine. Then i have a camera with a tag i created called OverviewCam. However when i run my program i get the error
NullReferenceException: Object reference not set to an instance of an object
My code for setting the camera is
Camera OverviewCam;
OverviewCam = GameObject.FindGameObjectWithTag("OverviewCam").GetComponent<Camera>() as Camera;
Anyone know how to fix this or another way to allow me to change cameras when going to a scene a certain way
1 $$anonymous$$ake sure the camera have the tag 2 try this
GameObject.FindGameObjectWithTag("OverviewCam").gameObject.GetComponent<Camera>();
Seems like like either the tag does not work or the component can't be found. Does the camera have a Camera component?
It seems clear the tag is not being found, so GetComponent is being called on a null object, generating the NullReferenceException. Start by separating these two (combining these calls is known to be a bit dangerous). Get the object from Find, store that return in a local variable, test it for null, and only then should you use it to call the GetComponent(). That said, you'll still have to debug why the tag isn't being found (is the V capital).
Answer by spooneystone · Aug 09, 2018 at 09:55 PM
OverviewCam = GameObject.FindGameObjectWithTag("OverviewCam").GetComponent<Camera>()
Ok. is this called in either Start() or Awake()??
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Player Sprite Changing Scenes And getting a new camera to follow it and default spawn position 1 Answer
Rotating player relative to the camera (Unity C#) 1 Answer
UNITY 3D: How to make the camera follow the player? Smoothly 2 Answers