How can I assign different object to different player in multuplayer before it needs?

I try to assign the object base on the following code. However, every time I tun the start in the network.
It show the “Null Reference Exception” about 3 times.
It seems that before I assign the object to the player, the update has already need this object.
How can I assign those objects to the player before it needs?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;
using System;

public class Player_ID : NetworkBehaviour
{

    [SyncVar] public string playerUniqueName;
    private NetworkInstanceId playerNetID;
    private Transform myTransform;

    public PlayerController playerController;

    public Score score_P1;
    public Score_P2 score_P2;


    public static bool playerConnect1;
    public static bool playerConnect2;

    public static string player1;
    public static string player2;

    private float a = 1f;
    public override void OnStartLocalPlayer()
    {
        GetNetIdentity();
        SetIdentity();
    }

    // Use this for initialization

    // Update is called once per frame
    void Update()
    {
        a -= Time.deltaTime;
        if (myTransform.name == "" || myTransform.name == "dragon_P1(Clone)")
        {
            SetIdentity();
        }
    }

    [Client]
    void GetNetIdentity()
    {
        playerNetID = GetComponent<NetworkIdentity>().netId;
        CmdTellserverMyIdentity(MakeUniqueIdentity());
    }

    void SetIdentity()
    {
        if (!isLocalPlayer)
        {
            myTransform.name = playerUniqueName;
        }
        else
        {
            myTransform.name = MakeUniqueIdentity();
        }

        if (playerConnect1 == false)
        {
            player1 = "Player" + GetComponent<NetworkIdentity>().netId;


            LayBombs lay = GameObject.Find(player1).GetComponent<LayBombs>();
            lay.rocket = GameObject.Find("NewRocket_P1").GetComponent<Image>();
            lay.bombHUD = GameObject.Find("NewBomb_P1").GetComponent<Image>();
            lay.lay = GameObject.Find("layBomb").GetComponent<layBombButton>();
            lay.rocketRefill = GameObject.Find("RocketRefil").GetComponent<RocketRefill>();


            PlayerHealth playerHealth = GameObject.Find(player1).GetComponent<PlayerHealth>();
            playerHealth.image = GameObject.Find("NewHealthBar_P1").GetComponent<Image>();

            playerConnect1 = true;

        }

        else if (playerConnect2 == false)
        {
            player2 = "Player" + GetComponent<NetworkIdentity>().netId;
            print(player2);
            PlayerHealth playerHealth = GameObject.Find(player2).GetComponent<PlayerHealth>();

            playerConnect2 = true;

            LayBombs lay = GameObject.Find(player2).GetComponent<LayBombs>();
            lay.rocket = GameObject.Find("NewRocket_P2").GetComponent<Image>();
            lay.bombHUD = GameObject.Find("NewBomb_P2").GetComponent<Image>();
            lay.lay = GameObject.Find("layBomb").GetComponent<layBombButton>();
            lay.rocketRefill = GameObject.Find("RocketRefil").GetComponent<RocketRefill>();

        }
    }


string MakeUniqueIdentity()
    {
        string uniqueName = "Player" + playerNetID.ToString();
        return uniqueName;
    }
    [Command]
    void CmdTellserverMyIdentity(string name)
    {
        playerUniqueName = name;
     
    }
 
}

Perhaps there is initialization code you’ve not posted here, but I don’t see anywhere in this code that myTransform has been initialized. If that’s true for all the rest of your code, myTransform would be null, and therefor myTransform.name would generate a null reference exception.


Since it is rare that one creates a Transform standalone, I’d assume somewhere you are acquiring the Transform of a GameObject, but you’d have to tell me. If your Transform is standalone, merely append “= new Transform();” where you declare myTransform, to something similar in your Start (or Awake) method.


That said, since this is also a network based system, perhaps you have a problem where Update fires frequently, long before network connections have an opportunity to initialize, such there couldn’t be a myTransform instantiated. This is a basic synchronization problem that applies in lots of situations, and the solution may be to simply check if myTransform is null, skipping any use of it in Update until such time that myTransform has been instantiated.