GetComponentScript

im trying to get a script from my player (PlayerHealth) but is not working…
i even put GetComponentsInChildren , but still not working…

thats my script:

public CameraFollow Camera;
public PlayerUI _playerUI ;

void OnJoinedRoom()
{
    CreatePlayerObject();
}

void CreatePlayerObject()
{
    Vector3 position = new Vector3(0f, 0.5f, 0f);

    GameObject newPlayerObject = PhotonNetwork.Instantiate("Player", position, Quaternion.identity, 0);
    Camera.target = newPlayerObject.transform;
    //the part which is not working :
    _playerUI._playerHealthScript = newPlayerObject.GetComponent<PlayerHealth>(); 
}

If the line giving you the error is this one

 _playerUI._playerHealthScript = newPlayerObject.GetComponent<PlayerHealth>(); 

Then your null reference is not the newPlayerObject, but the _playerUI. If the newPlayerObject was null then you would be seeing the null ref error from the line above.

Camera.target = newPlayerObject.transform;

You could confirm with a debug log for each object to check if it is null, but looking at this code, I’d look more to your _playerUI than your newPlayerObject reference