0
\$\begingroup\$

I have a confusion about when and where to use NetworkServer.RegisterHandler() and NetworkManager.singleton.client.RegisterHandler()? And also I am getting Unknown message ID 1002 error.

I have a message class

public class NetworkMsgHandler : NetworkBehaviour {

public const short movement_msg = 1002;

public class BulletMovementMsg : MessageBase {

    public string objectName;
    public Vector3 objectPosition;
    public Quaternion objectRotation;
    public float time;
}

}

I have ServerRelay which has ServerOnly authority has Script ServerRelay for registering Message Handler on Server.

public class NetworkServerRelay : NetworkMsgHandler {
private void Start()
{
    if(isServer)
    {
        RegisterNetworkMessages();
    }
}

private void RegisterNetworkMessages()
{
    NetworkServer.RegisterHandler(movement_msg, OnReceivePlayerMovementMessage);
}

private void OnReceivePlayerMovementMessage(NetworkMessage _message)
{

    BulletMovementMsg _msg = _message.ReadMessage<BulletMovementMsg>();
    NetworkServer.SendToAll(movement_msg, _msg);
}

}

Then I have Bullet GameObject it has script Projectile which is sending movement message this class is derived from NetworkMsgHandler.

public class NetworkProjectile : NetworkMsgHandler {


private void Start() {

    if (localPlayerAuthority) {

        canSendNetworkMovement = false;
        RegisterNetworkMsg();
    }
    else {

        isLerpingPosition = false;
        isLerpingRotation = false;

        realPosition = transform.position;
        realRotation = transform.rotation;
    }
}

private void RegisterNetworkMsg() {

    NetworkManager.singleton.client.RegisterHandler(movement_msg, OnReceiveMovementMsg);
}

private void OnReceiveMovementMsg(NetworkMessage networkMessage) {

    Debug.Log("Message received");
    BulletMovementMsg msg = networkMessage.ReadMessage<BulletMovementMsg>();

    if(msg.objectName != transform.name) {

        ProjectileManager.Instance.SpawnedBullets[msg.objectName].GetComponent<NetworkProjectile>().ReceiveMovementMsg(msg.objectPosition, msg.objectRotation, msg.time);
    }
}

public void ReceiveMovementMsg(Vector3 position, Quaternion rotation, float _timeToLerp) {

    //lerping between position
}

private void Update() {

        if(!canSendNetworkMovement) {

            canSendNetworkMovement = true;
            StartCoroutine(StartNetworkSendCoolDown());
        }
}

private IEnumerator StartNetworkSendCoolDown() {

    timeBetweenMovementStart = Time.time;
    yield return new WaitForSeconds((1 / networkSendRate));
    SendNetworkMovement();
}

public void SendNetworkMovement() {

    timeBetweenMovementEnd = Time.time;
    SendMovementMsg(bulletID, transform.position,transform.rotation, (timeBetweenMovementEnd - timeBetweenMovementStart));
    canSendNetworkMovement = false;
}

public void SendMovementMsg(string _bulletID, Vector3 position, Quaternion rotation, float timeToLerp) {

    BulletMovementMsg _msg = new BulletMovementMsg() {

        objectRotation = rotation,
        objectPosition = position,
        objectName = bulletID,
        time = timeToLerp
    };

    NetworkManager.singleton.client.Send(movement_msg, _msg);
}

}

So whenever a bullet spawns it generates an error on client side saying Unknown message ID 1002 connId:1 UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate()

\$\endgroup\$
  • \$\begingroup\$ What have you written so far? Can you show us a minimal, complete, verifiable example of your code that we can use to reproduce this error? \$\endgroup\$ – DMGregory Dec 28 '18 at 15:10
  • \$\begingroup\$ @DMGregory I've added the relevant code. Sorry for the late response. \$\endgroup\$ – Abhinay Singh Negi Jan 2 at 4:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.