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.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Having trouble instantiating object on ui.

Discussion in 'Scripting' started by KennethGames2020, Apr 5, 2020.

  1. KennethGames2020

    KennethGames2020

    Joined:
    Feb 26, 2020
    Posts:
    52
    Code (CSharp):
    1.                     Client newClient = new Client();
    2.                    
    3.                     newClient.username = receivedData.storedData;
    4.                     newClient.clientEndpoint = clientEP;
    5.  
    6.                     connectedClients.Add(newClient);
    7.  
    8.                     Debug.Log("Player : " + newClient.username + " : has connected. Sending connection confirmation...");
    9.  
    10.                     SendConfirmConnect(newClient.clientEndpoint);
    11.  
    12.                     Debug.Log("Adding to player list");
    13.                     AddToPlayerList(newClient.username);
    Code (CSharp):
    1.     public void AddToPlayerList (string newUsername)
    2.     {
    3.         GameObject newSlot = Instantiate(playerSlotObject, playerPanel.transform.position, Quaternion.identity);
    4.         Text slotText = newSlot.GetComponentInChildren<Text>();
    5.         slotText.text = newUsername;
    6.         newSlot.transform.SetParent(playerPanel.transform, false);
    7.         activePlayerSlotObjects.Add(newSlot);
    8.         Debug.Log("Player slot added...");
    9.         UpdatePlayerList();
    10.     }
    The debug shows:
    "Adding to player list"

    But the "newSlot" object is not spawned and the debug does not show:
    "Player slot added..."

    can someone please tell me what is going on?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,970
    Is that slot object part of a UI hierarchy, like under a Canvas?

    If so, you should be using this form of Instantiate, the one that takes an optional transform as parent, and you should be immediately parenting the spawned object into the hierarchy, not later on.

    Code (csharp):
    1. GameObject newSlot = Instantiate<GameObject>(
    2.                    playerSlotObject, playerPanel.transform);
    Do that only, run it, see where it comes up. It should be dead-center, assuming no layout components above it driving it to a position. From there you can either position it explicitly to where a blank GameObject marks where it goes, or else use a layout of some type (Grid, vertical, horizontal, etc), or however you want to move it around.
     
  3. KennethGames2020

    KennethGames2020

    Joined:
    Feb 26, 2020
    Posts:
    52
    AddToPlayerList()

    is not being called at all. The debug inside the function does not show up in the console.
     
  4. KennethGames2020

    KennethGames2020

    Joined:
    Feb 26, 2020
    Posts:
    52
    Huh, so I did a test. I built the server console for android and put it on my phone. The newSlot shows up on the phone, but not when I run it in the editor. Any reason why this may be happening?

    EDIT:

    If it helps here is a larger part of the code:

    Code (CSharp):
    1. void Receive(IAsyncResult result)
    2.     {
    3.         try
    4.         {
    5.             EndPoint clientEP = new IPEndPoint(IPAddress.Any, 0);
    6.             int dataLen = 0;
    7.             byte[] data = null;
    8.             try
    9.             {
    10.                 dataLen = serverSocket.EndReceiveFrom(result, ref clientEP);
    11.                 data = new byte[dataLen];
    12.                 Array.Copy(byteData, data, data.Length);
    13.             }
    14.             catch
    15.             {
    16.                 return;
    17.             }
    18.             finally
    19.             {
    20.                 EndPoint clientEP2 = new IPEndPoint(IPAddress.Any, 0);
    21.                 serverSocket.BeginReceiveFrom(byteData, 0, byteData.Length, SocketFlags.None, ref clientEP2, Receive, clientEP2);
    22.             }
    23.  
    24.             string received = Encoding.UTF8.GetString(data);
    25.             DataTransport receivedData = JsonUtility.FromJson<DataTransport>(received);
    26.  
    27.             if(receivedData.dataType == "Connect Request")
    28.             {
    29.                 Debug.Log("A player is attempting to connect to the server...");
    30.                 if(CanConnect(receivedData.storedData, clientEP))
    31.                 {
    32.                     Client newClient = new Client();
    33.                     //The stored data in the transport should be the player's username:
    34.                     newClient.username = receivedData.storedData;
    35.                     newClient.clientEndpoint = clientEP;
    36.  
    37.                     connectedClients.Add(newClient);
    38.  
    39.                     Debug.Log("Player : " + newClient.username + " : has connected. Sending connection confirmation...");
    40.  
    41.                     SendConfirmConnect(newClient.clientEndpoint);
    42.  
    43.                     Debug.Log("Adding to player list...");
    44.                     AddToPlayerList(newClient.username);
    45.  
    46.                 } else
    47.                 {
    48.                     Debug.Log("Player : " + receivedData.storedData + " : failed to connect...");
    49.                 }
    50.             } else if (receivedData.dataType == "Disconnect Request")
    51.             {
    52.  
    53.             } else if(receivedData.dataType == "Object")
    54.             {
    55.                 UpdateObject(receivedData);
    56.             } else if(receivedData.dataType == "Chat")
    57.             {
    58.                 ChatMessage newChat = JsonUtility.FromJson<ChatMessage>(receivedData.storedData);
    59.                 Debug.Log("Chat: " + newChat.sendingUsername + ": " + newChat.text);
    60.             }
    61.  
    62.         }
    63.         catch
    64.         {
    65.             return;
    66.         }
    67.     }
     
    Last edited: Apr 5, 2020