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);
}
}
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?
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.
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.