spawning player object without network manager.

Hi
I’m just trying out Unity MultiPlayer and when I try to use my own code instead of network manager to spawn player objects then syncing doesn’t work from client to server. the local client in the host works but not the other one.
also how can I read the content of AddPlayerMessage from the received message. there seem to be no class for that.
here is my code

using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.Networking.Types;
using UnityEngine.Networking.Match;
using System.Collections.Generic;

public class HostGame : NetworkMatch
{
    public bool useMatchMaking;
    public NetworkIdentity prefab;
    List<MatchDesc> matchList = new List<MatchDesc>();
    bool matchCreated;
    short pid;

    void Start()
    {
        Application.runInBackground = true;
    }

    void OnGUI()
    {
        // You would normally not join a match you created yourself but this is possible here for demonstration purposes.
        if (GUILayout.Button("Create Room"))
        {
            SetUpServer();
           
        }

        if (GUILayout.Button("List rooms"))
        {
            SetupClient();
        }

       
    }

    void SetUpServer()
    {
        NetworkServer.Listen(9000);
        NetworkServer.RegisterHandler(MsgType.AddPlayer, s =>
        {
            GameObject go = Instantiate(prefab.gameObject, Vector3.zero, Quaternion.identity) as GameObject;
            NetworkServer.AddPlayerForConnection(s.conn, go, 1);
        });
        var myClient = ClientScene.ConnectLocalServer();
        myClient.RegisterHandler(MsgType.Connect, OnConnected);
    }

    void SetupClient()
    {
        ClientScene.RegisterPrefab(prefab.gameObject);
        NetworkClient myClient = new NetworkClient();
        myClient.RegisterHandler(MsgType.Connect, OnConnected);
        myClient.Connect("localhost", 9000);

    }

   

    public void OnConnected(NetworkMessage msg)
    {
        Debug.Log("Connected!");
        ClientScene.Ready(msg.conn);
        ClientScene.AddPlayer(1);
       
    }
}

just a bump since all uNet developers in unity technologies were not available in weekend.

There is a message class for it:

void OnAddPlayer(NetworkMessage netMsg)
{
    var addPlayerMsg = netMsg.ReadMessage<AddPlayerMessage>();
    NetworkServer.AddPlayerForConnection(netMsg.conn, addPlayerMsg.playerControllerId);
}

So not reading the controllerID was causing my stream to be mixed up with other stuff due to stream pulling/using one stream for multiple purposes?
I think the docs need improvement on these to be more clear.

Also if you mention when deriving from MessageBase what things are serialized automatically if we don’t write a custom serializer.

About this NetMessage , isn’t it good to issue a warning or something when one does it incorrectly?

Ah and it doesn’t solve it

using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.Networking.Types;
using UnityEngine.Networking.NetworkSystem;
using UnityEngine.Networking.Match;
using System.Collections.Generic;

public class HostGame : NetworkMatch
{
    public bool useMatchMaking;
    public NetworkIdentity prefab;
    List<MatchDesc> matchList = new List<MatchDesc>();
    bool matchCreated;
    short pid;

    void Start()
    {
        Application.runInBackground = true;
    }

    void OnGUI()
    {
        // You would normally not join a match you created yourself but this is possible here for demonstration purposes.
        if (GUILayout.Button("Create Room"))
        {
            SetUpServer();
           
        }

        if (GUILayout.Button("List rooms"))
        {
            SetupClient();
        }

       
    }

    void SetUpServer()
    {
        NetworkServer.Listen(9000);
        NetworkServer.RegisterHandler(MsgType.AddPlayer,AddPlayerServer);
        var myClient = ClientScene.ConnectLocalServer();
        myClient.RegisterHandler(MsgType.Connect, OnConnected);
    }

    void AddPlayerServer(NetworkMessage s)
    {
        var n = s.ReadMessage<AddPlayerMessage>();
        GameObject go = Instantiate(prefab.gameObject, Vector3.zero, Quaternion.identity) as GameObject;
        NetworkServer.AddPlayerForConnection(s.conn, go, n.playerControllerId);

    }

    void SetupClient()
    {
        ClientScene.RegisterPrefab(prefab.gameObject);
        NetworkClient myClient = new NetworkClient();
        myClient.RegisterHandler(MsgType.Connect, OnConnected);
        myClient.Connect("localhost", 9000);

    }

   

    public void OnConnected(NetworkMessage msg)
    {
        Debug.Log("Connected!");
        ClientScene.Ready(msg.conn);
        ClientScene.AddPlayer(1);
       
    }
}

I’m using 5.1P3 and can not find any settings for APPID in player settings, am not using match maker however. I wondered that might be the issue since unity complaigns about that.

the only other script which I have is a game object controller which works with network manager.

That code works when I ran it. What exactly is not working?

The local client gets instantiated and the transform is synced when I move it (there is no error in console for it) the prefab has a rigidbody with simple AddForce using keyboard input for different directions in FixedUpdate.
When I build the project and run an exe client and connect to the host.
the position of this second client which is not local in the host is not updated and NetworkTransform component spits out lNull Reference Exceptions every frame (or network tick maybe) to the console.

When I use the network manager to spawn the object the problem doesn’t exist at all.

Can you provide a stack trace for the exception from the editor log?

this is the stack trace on server when client rigidbody moves this happens.


NullReferenceException: Object reference not set to an instance of an object
UnityEngine.Networking.NetworkTransform.HandleTransform (UnityEngine.Networking.NetworkMessage netMsg) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkTransform.cs:1048)
UnityEngine.Networking.NetworkConnection.HandleMessage (System.Collections.Generic.Dictionary`2 handler, UnityEngine.Networking.NetworkReader reader, Int32 receivedSize, Int32 channelId) (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkConnection.cs:302)
UnityEngine.Networking.NetworkServer.InternalUpdate () (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkServer.cs:652)
UnityEngine.Networking.NetworkServer.Update () (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkServer.cs:514)
UnityEngine.Networking.NetworkIdentity.UNetStaticUpdate () (at C:/buildslave/unity/build/Extensions/Networking/Runtime/NetworkIdentity.cs:724)


the client doesn’t have any errors.

the project is attached as well.

2245090–149943–uNet test.zip (28 KB)

I dont get any errors when I run that project. Try upgrading to latest patch release of the editor.

ah really, I was using p3 so I should get the latest.

oh shoot I was on 5.1.1 p3 , sorry for taking your time, will give back here in the forum :slight_smile:

@seanr well it doesn’t work on latest 5.1.2p3 as well. here it is a video on drop box
https://dl.dropboxusercontent.com/u/56980978/unet%20bug.mp4

the rigidbody doesn’t still sync,
there seem to be a similar issue at

I mean my problem is with rigidbody syncing and NetworkTransform as well. I can send commands and rpcs easily

I tried it with a 5.1 build and reproduced the issue.

The line “ClientScene.AddPlayer(1);” is exposing a bug in NetworkTransform that is fixed in 5.2. If you do “ClientScene.AddPlayer(0);” it will work.

Thanks,