Below is a simple script. I use it in 2 separate executions of Unity.
In the first one, I click “Create Game” and become a server. In the
second, I click “Join Game” and become a client. If I then click
“Make Cube” first in the server, both server and client see the new cube.
I can also then click “Make Cube” in the client and both see the second
cube as well.
However, if I click “Make Cube” first in the client, the cube is not
created and this is printed at the cient:
Failed Network.Instantiate because we are not connected.
If you understand why this occurs, please let me know.
Thanks.
–ralph
var prefab_cube : GameObject;
function OnGUI() {
if (GUILayout.Button(“Create Game”))
{
Network.useNat = false; // !Network.HavePublicAddress();
Network.InitializeServer(5,25002);
}
if (GUILayout.Button(“Make Cube”))
{
var x = Random.value;
var sign = Random.value;
if (sign > 0.5) x = -1;
Network.Instantiate(prefab_cube, Vector3(x5,0,0),
Quaternion.identity, 0);
}
if (GUILayout.Button(“Join Game”))
{
Network.Connect(“localhost”,25002);
}
}
You connect to the server before pushing “Make Cube”, right? If I use your exact script it works as it should, the cube is instantiated. You need to connect to the server before using Network.Instantiate.
I see the problem, now. When you click Join, the server is paused in the background, so it doesn’t accept your connection until it unpauses. You have to make the Server run in the background, or switch back to the server and back to the client.
Edit->Project Settings->Player->Run in background checkbox.
Ah yes. You are so right. If I start the server, start the client, switch
back to server for just a moment, and the switch back to the client
and do the make cube, all works great. This is really helpful.
Thank you so very much for the help!
–ralph