How to check when a new client/local player is loaded?

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

Finally I solved this by doing it a different way.

Using OnStartLocalPlayer, a [Command] is called on the Sever that calls an [RpcClient].

The [RpcClient] calls a script that exists on all UI HealthBars. This script sets the LookAt using:

transform.LookAt(Camera.main.transform);

Not sure if there is a ´more´correct/efficient way…but this approach is working well, so I put it here in case someone else is trying to do a similar thing.

However, if someone has any better suggestions, please let us know.

Thanks