Networking-multplayer issue

hi ther guys first thanks for those who read and answer my quesions …!! :slight_smile:
now the isue i have is this:

so i network.instantiate my player o the level like this in the OnNetworkLoadedLevel function that comes with unity:

void OnNetworkLoadedLevel ()
{
		int SpawnIndex = Random.Range(0, respawns.Length);
		
		GameObject this_player = Network.Instantiate(Player, 
													 respawns[SpawnIndex].transform.position,
									                 Quaternion.identity, 0) as  GameObject;
		
		this_player.GetComponent<IsRemotePlayer>().UserName = this_player_nametag;
}

UserName and this_player_nametag are strings but as you can se UserName is on a script attached to the player prefab i instantiate
, the isue is here on this line:

this_player.GetComponent().UserName = this_player_nametag;

it gives me this error on console:

NullReferenceException: Object reference not set to an instance of an object

β†’ so the question is why s this happening? i call properly the OnNetworkLoadedLevel() and even the player prefab spawns and sete everything properly, but the error that display the console is like there was no player gameobject at all right??

plz help me with this…!!

Several possibilities : this_player is null, this_player does not contain a script IsRemotePlayer, or the script IsRemotePlayer has no public declared variable named UserName

You need to check if everything exists :

void OnNetworkLoadedLevel ()
{
    int SpawnIndex = Random.Range(0, respawns.Length);
 
    GameObject this_player = Network.Instantiate(Player, 
                                   respawns[SpawnIndex].transform.position,
                                         Quaternion.identity, 0) as  GameObject;
 
    if (this_player)
    {
        Debug.Log("I have my player");
        IsRemotePlayer myScript = this_player.GetComponent<IsRemotePlayer>() as IsRemotePlayer;
        if (myScript)
        {
            Debug.Log("I have my script");
            string myCurrentName = this_player.GetComponent<IsRemotePlayer>().UserName;
             Debug.Log("I have my user name");
             this_player.GetComponent<IsRemotePlayer>().UserName = this_player_nametag;
        }
    }
}