Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

question about Network.Instantiate

Discussion in 'Multiplayer' started by rmbunity, Dec 10, 2007.

  1. rmbunity

    rmbunity

    Joined:
    Dec 1, 2007
    Posts:
    32
    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);
    }
    }
     
  2. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
    It happens here, too. I don't know why. It seems like a Unity bug to report with this example.

    -Jon
     
  3. larus

    larus

    Unity Technologies

    Joined:
    Oct 12, 2007
    Posts:
    274
    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.
     
  4. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
    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
     
  5. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
    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
     
  6. rmbunity

    rmbunity

    Joined:
    Dec 1, 2007
    Posts:
    32
    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