- Home /
NetworkManager auto deleting client gameobject when calling it
Hi everyone, first I want to tell that my english is not perfect.
So here is my problem : I have a _GM gameobject with a NewtorkManager herited script attached, and for ClientRpc methods I use the automatically generated client gameobject. I have already done a button in my UI that call a ClientRpc method on the client gameobject to synchronize some variables through all clients, it works.
To access the player gameobject I did a static function that return the instance of the player's gameobject . NetworkPlayer is the script attached to players gameobjects. Buuut when I try to call the
NetworkPlayer.PlayerGameobject.GetComponent<NetworkPlayer>().RpcTellClientPlayerNumber(numberOfPlayer);
I get an error : Object reference not set to an instance of an object.
If I delete the line from the NetworkManager herited script, the client's gameobject appear in my scene, if I call the gameobject, it will disappear.
There is no lines of code into the "RpcTellClientPlayerNumber(int a)" method ...
Idk what to do. Is the static return of the gameobject a problem ? That just make no sense.
Thank you for reading this and eventually for a response.
PS: My NetworkManager herited script & my networkplayer script : http://image.noelshack.com/fichiers/2016/02/1452692996-aze.png <= The LAST line of code http://image.noelshack.com/fichiers/2016/02/1452692996-qsd.png http://image.noelshack.com/fichiers/2016/02/1452693199-wxcv.png http://image.noelshack.com/fichiers/2016/02/1452693145-wxc.png
Answer by ashleyjlive · Jan 14, 2016 at 10:28 PM
I believe the problem lies with PlayerGameobject being null as it is not set before you call this line of code. Remove the line "NetworkPlayer.PlayerGameobject.GetComponent().RpcTellClientPlayerNumber(numberOfPlayer);"
Insert this into your ServerManager script.
public static ServerManager s_Singleton;
void Awake () { s_Singleton = this; }
Then in the Start method of the NetworkPlayer Script insert
if(isServer) { RpcTellClientPlayerNumber(ServerManager.s_Singleton.numberOfPlayer); }
See if that works. Sorry for the code formating.
Thank you for the anwser, I understand better what I did wrong, the "start" method of the networkplayer script ofcourse is executing after the OnServerConnect event, didnt realized that. I tried what you told me to do, but now I have an error at the very first line of my networkmanager's start script
void Start ()
{
Network$$anonymous$$anager.singleton.networkPort = Random.Range(7777, 32000); <= I get the error of Object reference on this line
Debug.LogError(Network$$anonymous$$anager.singleton.networkPort);
Network$$anonymous$$anager.singleton.StartHost();
}
and when I put the "void Awake () { s_Singleton = this; }" into commentary, the problem disappear, it is like this line is preventing player from spawning. I will search myself but if you can help again I will appreciate it, thanks anyway
Edit : The problem does come from the "Awake" call, it is weird but it seems to work. Thank you ! If you can explain why the Awake fonction make the error that would be nice :)
Your answer
![](https://koobas.hobune.stream/wayback/20220612060047im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Network RCP between client server projects 0 Answers
ClientRPC not getting sent. 0 Answers
Why is my script variable NULL in a ClientRPC??? 1 Answer
iOS IL2CPP Networking Issues Unity 5.0.x 3 Answers