Hi there, I am having an issue with a game I am developing in Unity, with Mirror. My problem is the spawning of UI Prefabs, specifically a scorecard object with a raw image and a text element, within a canvas. This works perfect for the host, but fails client-side (where the UI element isn’t made a child of the canvas and thus doesn’t display). The code resulting in this behaviour is as follows:
[SerializeField] private GameObject scoreCardElement;
[SerializeField] private Transform canvas;
bool scoreCardCreated;
private void Update(){
if (!scoreCardCreated) InstantiateUIScoreCard();
}
private void InstantiateUIScoreCard(){
scoreCardElement = Instantiate(scoreCardElement, canvas);
NetworkServer.Spawn(scoreCardElement);
scoreCardCreated = true;
}
I choose to instatiate these objects rather than have a preset amount as the lobby size is variable for my game scene.
I understand where this problem originates, and I’m aware of the common issue surrounding the parenting of spawned network objects (across several networking frameworks: MGO, Photon, Mirror etc.). Typically, when faced with parenting issues in networking, it seems that individuals choose to simply workaround it, like setting the transform’s position and rotation to that of the parent. The only issue is, parents in UI are important, and if the UI element isn’t a child of the canvas, for example, it will not render. This will prove very incovenient in future, especially with layout components where parenting is critical.
Surely this must be a fairly common task in the development of online multiplayer games, with the creation and destruction of UI elements on a canvas.
I am stumped, any help would be greatly appreciated. In the worst case, I am open to simply having a preset number of score cards, but this is fairly limiting and doesn’t solve the problem for future operations.
Thanks in advance!