I’m playing around with Unity’s networking from v5.1, and I’ve run into a problem with 2D where I need to spawn the characters under the canvas. This works on the server side, but is not working correctly on the client side. On the client side, the character transforms never have their parent set. From what I can tell, the client never actually runs the code below for some reason. I can’t seem to find which method to override for the client side of things. My current spawning code test looks like this:
public class NetworkManagerTwoD : NetworkManager
{
public Transform CanvasParent = null;
public override void OnServerAddPlayer(NetworkConnection conn, short playerControllerId)
{
// Instantiate the player
GameObject player = Instantiate(playerPrefab);
// Set the parent to be the canvas. WorldPositionStays should be false so that it retains correct size relative to the canvas.
player.transform.SetParent(CanvasParent, false);
// Get the RectTransform component of the instantiated tile.
RectTransform rectTransform = player.GetComponent<RectTransform>();
// Set the position to be the calculated value.
rectTransform.anchoredPosition = Vector2.zero;
// Let the network manager do its magic.
NetworkServer.AddPlayerForConnection(conn, player, playerControllerId);
}
}
The players can move around just fine, but since the client doesn’t have the objects in the correct canvas (just in the hierarchy unparented) they can’t see the players walking around. Any idea what concept I am missing here?