- Home /
Networking-multplayer issue
hi ther guys first thanks for those who read and answer my quesions ..!! :) 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..!!
Not necessarily. this_player could well be valid! but it may not have an IsRemotePlayer script assigned to it. Is this script showing in the editor when you click on the player game object? I would recommend checking both the value of this_player for null and the value of GetComponent return for null anyway.
Answer by KiraSensei · Nov 21, 2013 at 02:34 PM
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;
}
}
}
Your answer
Follow this Question
Related Questions
How to create a object that can only be seen by one player? 0 Answers
Instantiate prefab and passing parameter 1 Answer
Unet NetworkServer.Spawn() not working 5 Answers
ChannelBuffer buffer limit of 16 packets reached on sending images 1 Answer
How can I spawn a GameObject on all players in my multiplayer game? 0 Answers