- Home /
Server and Network no communication
I have a problem with the Network, i create a server and client can connect to it. But i want get some parameters from server to client i tryed this:
public GameObject server; //have tag "Test" and starting value in number is 0
private GameObject finded;
if (GUILayout.Button("StartServer")) {
server.GetComponent<Test_Script3> ().number = 250;
Network.Instantiate(server, transform.position, transform.rotation, 1);
}
if (Network.peerType == NetworkPeerType.Client) {
finded = GameObject.FindGameObjectWithTag("Test");
if (finded != null) GUILayout.Label("O: " + finded.GetComponent<Test_Script3>().number);
} else if (Network.peerType == NetworkPeerType.Server) {
finded = GameObject.FindGameObjectWithTag("Test");
if (finded != null) GUILayout.Label("O: " + finded.GetComponent<Test_Script3>().number);
}
The main problem is that value of parameter number on the server is 250 but on the client is 0;
Answer by Bunny83 · May 08, 2014 at 11:44 AM
It doesn't work like that. Each player / peer has it's own game world and it's own prefabs. If you change a number on the prefab in the server it won't change the prefab on any other client.
Some basic points about networking in Unity:
Only NetworkViews will be able to transfer data from one PC to another.
NetworkViews on different PCs / peers are liked by their NetworkViewID. Those with the same ID can communicate with each other
A NetworkView can "observe" a built-in component and have it's properties synchronised.
Only the NetworkView owner can transmit state-updates, everybody else just listens / receives.
Apart from state-synchronisation every NetworkView can be used to transmit RPC calls bi-directional. So a client can send an RPC to the server and the server to the client(s)
You can't send RPCs without having a linked NetworkView pair / group. The only indirect exception is Network.Instantiate which uses an RPC behind the scenes but doesn't require a NetworkView.
NetworkViews can also observe a custome script component (aka one of your MonoBehaviour-scripts), but you have to implement the OnSerializeNetworkView callback in that script and specify which data should be transmitted / received.
NetworkViews on objects placed in a scene are linked automatically when the client loads the scene.
I suggest to read through the documentation of NetworkView, Network and the Networking basics. Networking isn't that complicated, but it takes some time until you understand it throughout, so don't rush, read and learn.
edit
I just added a little code snippet that would do what you want in your question: Transferring a number from the Peer that Instantiated the object to all other clients:
When instantiating:
int num = 250;
server.GetComponent<Test_Script3>().number = num;
GameObject instance = (GameObject)Network.Instantiate(server, transform.position, transform.rotation, 1);
instance.networkView.RPC("SetNumber", RPCMode.AllBuffered, num);
Inside a script on the "server" object you have to declare this method:
[RPC]
void SetNumber(int aNum)
{
GetComponent<Test_Script3>().number = aNum;
}
Note: I used a BufferedRPC instead of a "normal" RPC since Network.Instantiate also uses a buffered one. So it's ensured that when another client joins later the value is set as well.
Personally i don't like buffered RPCs all together because they almost force you to use them everywhere and Unity doesn't provide a good way to keep track of / deleting old buffered RPCs.
"Test_Script3" and "number" aren't really meaningful names so we have no idea what kind of information you try to pass to the client. You have to decide from case to case which kind of RPC is the best one.
I work with the Network only few weeks, so i dont really get how it work, but your code really helped me and it is working now. I am so happy and glad that you helped me, thank you so much. Thank you!
Your answer
Follow this Question
Related Questions
Networking RPC calls never sent to other clients 1 Answer
Smooth network communication like Unity remote 1 Answer
How to hide a started/full server 0 Answers
how to create a simple udp network to send text data c# 0 Answers
Do i need to have 2 seperate apps communicating for server/client relationship? 2 Answers