- Home /
Why does checking if Camera.Main is non-null generate an assertion failed message?
I'm trying to add a line in my character controller that checks if the main camera exists, and turns the character relative to that camera:
bool relativeToCamera = true;
if (relativeToCamera && Camera.main !=null)
{
relativeTransform = Camera.main.transform;
}
This works fine while in-game, but every time I quit out of play mode, I get two identical errors pointing at this line: Assertion failed: Assertion failed on expression: 'go.IsActive() && go.GetTag() != 0' UnityEngine.Camera:get_main()
It specifically points to my conditional check, not the line setting the relative transform, which makes me think that checking if Camera.main !=null is the problem. However, the act of checking whether something is null shouldn't cause any problems, it's only trying to access it that would be an issue. Is there something I can change here, or is this one of Unity's harmless eccentricities?
I would suggest you try reproducing this issue an otherwise blank project. If you are able to do this, I would suggest submitting it as a bug.
Answer by Legend_Bacon · Nov 24, 2017 at 05:39 PM
Hello there,
When you type in "Camera.main", I'm guessing Unity searches for a Camera with the "main" tag. But you can't get the tag of a GameObject that's inactive, so it gives you an error. Since this only happens when quitting play mode, I am guessing you have that line in an Update loop or OnDestroy call?
An easy fix would be to assign a Camera variable on Start() to "Camera.main", and then just check if that variable is null or not. That should fix it, but if it doesn't please let me know.
Hope that helps!
~LegendBacon
Note: This will work only if you don't change the main camera. (unusual, but can happen)
Your answer
![](https://koobas.hobune.stream/wayback/20220612143259im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to make camera position relative to a specific target. 1 Answer
How can I produce the typical FPS mouse steering effect? 2 Answers
Show other players other models 1 Answer
This camera controller is overriding it's default position when the boundary check is enabled? 0 Answers
How to create texture from multiple sprite in unity2d? 0 Answers