Unity 5 UNet Spawn as Child.

Hello everyone !

I got a problem trying to instantiate a GameObject across the network as a child on the “Canvas” GameObject, trying to make a UI like what is shown in the Unity 5 UNet presentation (See below.).

Also , instead of LocalPlayer authority, almost everything is handled by the server.

In the video, they create an UI with the Legacy system, I’m trying to create one from a Prefab with the new UI system, hence why I need to spawn the UI as child of the “Canvas” GameObject.

From what I already got, Unity doesn’t do it natively.

I already read this answer (UNET Spawn object as a child object. - Unity Answers) and implemented it, but the ID never gets reconized.

I did called NetworkServer.RegisterHandler(…) by creating an AdvancedNetworkManager that heritate from NetworkManager, and overrided the void OnClientConnect(NetworkConnection conn) function.

Here’s some screens and code :

AdvancedNetworkManager.cs :

public class AdvancedNetworkManager : NetworkManager
{
    public override void OnClientConnect(NetworkConnection conn)
    {
        base.OnClientConnect(conn);

        Debug.Log("Registering Server callbacks ...");
        Debug.Log("Registered Message (ID : " + Message.SetParent + ")");
        NetworkServer.RegisterHandler(Message.SetParent, SetParentMessage.OnSetParent);
    }
}

Utils.cs :

public class Utils
{
    public static void LinkToParent(GameObject child, GameObject parent, bool stay)
    {
        child.transform.SetParent(parent.transform, false);
        Debug.Log("Link to parent called (ID : " + Message.SetParent + ")");
        NetworkServer.SendToAll(Message.SetParent, new SetParentMessage(child, parent, stay));
    }
}

Message.cs :

public class Message
{
    // used for auto-increment
    private enum Type
    {
        SetParent = MsgType.Highest + 1
    }

    public const short SetParent = (short)Type.SetParent;
}

public class SetParentMessage : MessageBase
{
    public NetworkInstanceId netId;
    public NetworkInstanceId parentNetId;
    public bool woldCoordStay;

    public SetParentMessage()
    {
    }

    public SetParentMessage(GameObject gameObject, GameObject parent, bool stay)
    {
        netId = GetNetId(gameObject);
        parentNetId = GetNetId(parent);
        woldCoordStay = stay;
    }

    private static NetworkInstanceId GetNetId(GameObject obj)
    {
        // NOTE: no error handling
        return obj.GetComponent<NetworkIdentity>().netId;
    }

    public static void OnSetParent(NetworkMessage netMsg)
    {
        Debug.Log("OnSetParentCalled (Finally ...)");
        SetParentMessage msg = netMsg.ReadMessage<SetParentMessage>();
        ClientScene.objects[msg.netId].transform.SetParent(ClientScene.objects[msg.parentNetId].transform, msg.woldCoordStay);
    }
}

The code Intatianting the UI is only called on the server and is as followed :

uiInstance = Instantiate<GameObject>(combatUI);
    
NetworkServer.Spawn(uiInstance);
    
Utils.LinkToParent(uiInstance, canvas, false);

Here’s the Log I’m getting on the Client :
alt text

And here’s the Log I’m getting on the Server(Which is also a Client) :

I really don’t get it, is there something wrong in this ?

I hope I’m clear enough.

Thanks for your time. This is getting me crazy !

(UNet presentation : Unite 2014 - New Unity Networking in 5.x - YouTube)

Oh well, I found that replacing

NetworkServer.RegisterHandler(Message.SetParent, SetParentMessage.OnSetParent);

by

client.RegisterHandler(Message.SetParent, SetParentMessage.OnSetParent);

Gets rid of the unknown ID error, however, it sill gave me a ton of errors.

I just re-implemented my GUI so that every player render every other players UI, now that I think of it , I should have done that immediately.

Oh well …

Here’s a link that helped me : c# - Why will my server not execute a command sent by the client in Unity 5.1? - Game Development Stack Exchange