I have an object on the Server with basic a UI health bar attached. The health bar is set to always look at the camera of the Player by using the following code in the Update function:
transform.LookAt(Camera.main.transform);
When running on the Local Client, it works without any issue. However, when running on a Remote Client it produces the following error:
NullReferenceException: Object reference not set to an instance of an object
This error is generated 8 to 10 times and then stops. However, it still works ok. The health bar will always look at the camera of the Player as intended.
I think the reason is that initially the code cannot find the Camera (or the Camera is not available) until the Client/Player is loaded. I did a test by using a key-press to activate the above code and the result was that no error was generated.
Therefore, assuming I am correct and it relates to the timing of the Client/Player loading, how can I avoid this? Is there some way to check that a new Client/Player is loaded before running the above code? I tried several things like using OnStartClient, OnConnectToServer, but without success, so any help would be appreciated.
Thanks