- Home /
Why is my script variable NULL in a ClientRPC???
Here is my situation: I have a private variable that contains a Monobehaviour. When the connection is established, this script is found. When an RPC is called in which I want to use this script on a client(not for the server!), it returns NULL?!
The script is not being destroyed nor being set to NULL anywhere in my game.
public override void OnStartLocalPlayer () {
builderScript = GameObject.Find ("Terrain").GetComponent<TowerBuilder> ();
Debug.Log (builderScript);//this works
}
[ClientRpc]
void RpcBuildTower(Vector3 buildPos, Quaternion buildRot, Color towerC){
Debug.Log (builderScript);//prints "NULL"
builderScript = GameMaster.instance.builderScript;//I have to find it again to get it working
Debug.Log (builderScript);//prints "TowerBuilder" again
builderScript.BuildTower (buildPos, buildRot, towerC);
}
Any help is appreciated!
Could you add your NullReference error? Just need more context to better understand what the issue might be
I think it's a different client you receive the NullReferenceException for. Are you running Host and Client? Do an isLocalPlayer check in the RPC too.
Answer by TheItCrOw · Jan 30, 2018 at 12:14 AM
ClientRPC runs the function on all Clients using data from the server.
When you fill your builderScript with the GetComponent method, you do this as the LocalPlayer only. The Server has no data, that the variable builderScript has got a reference to your TowerBuilder. Thats why its null.
Your answer
Follow this Question
Related Questions
Network how to send data/activate function 1 Answer
Player Doesn't Exist On Client 0 Answers
RPC network info not returning an IP? 0 Answers
ClientRpc not called on Client 1 Answer
Requesting data from server 1 Answer