How to fix “Please initialize Cardboard XR loader before calling this function” error?

Please initialize Cardboard XR loader before calling this function. UnityEngine.Debug:LogError (object) Google.XR.Cardboard.Api:UpdateScreenParams () (at ... Cardboard Startup.cs:70)

If you are getting the above error when you run your Cardboard XR project in the Editor, you need to follow the steps below. Open CardboardStartup.cs and go to line 70.

Api.UpdateScreenParams();

Find the code above and replace it with the codes below.

#if !UNITY_EDITOR
Api.UpdateScreenParams();
#endif