question about Network.Instantiate

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(x
5,0,0),
Quaternion.identity, 0);
}
if (GUILayout.Button(“Join Game”))
{
Network.Connect(“localhost”,25002);
}
}

It happens here, too. I don’t know why. It seems like a Unity bug to report with this example.

-Jon

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.

Yes, it’s connected as he said. Using the steps written, this is 100% reproducible here.

Step 1: Start a server in one copy, and Join Game in the other copy.
Step 2: Click Make Cube in the client.

This produces the error. To produce the expected behavior, replace Step 2 with:

Step 2: Click Make Cube in the Server

Cheers,
-Jon

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.

Cheers,
-Jon

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