When starting a host game, I notice that in our Player aiming logic we get the mouse position on screen by calling Camera.main.ScreenToWorldPoint();
Would it be possible to assign the Camera to a variable to cache it in memory rather than calling it on every LateUpdate? I know calling Camera.main can be an expensive operation.
Currently doing something like:
private Camera mainCamera;
void Start(){
mainCamera = Camera.main;
}
private void RotateTurret(){
if (!IsOwner) { return; }
Vector2 turretPosition = inputReader.MousePosition;
Vector2 mousepositionOnScreen = mainCamera.ScreenToWorldPoint(turretPosition);
turretTransform.up = new Vector2(
mousepositionOnScreen.x - turretTransform.position.x,
mousepositionOnScreen.y - turretTransform.position.y
);
}
will cause a null reference exception when attempting to host a game with the error showing that the Camera has been destroyed but that the script is still trying to access it.