Search Unity

  1. Are you interested in providing feedback directly to Unity teams? Sign up to become a member of Unity Pulse, our new product feedback and research community.
    Dismiss Notice

Help Wanted RegisterHandler() isn't calling method | Mirror

Discussion in 'Multiplayer' started by uotsabchakma, Jul 4, 2021.

  1. uotsabchakma

    uotsabchakma

    Joined:
    Sep 17, 2019
    Posts:
    84
    I'm making a multiplayer game with Mirror networking.
    As Mirror don't have audiosource for network, I'm doing my own with a little lines of code.
    Now having a problem with RegisterHandler() which isn't calling a method as it should be. Am I wrong somewhere or RegisterHandler() has changed or modified? Here are the codes that I'm using:
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using Mirror;
    6. namespace Mirror.NetworkAudio
    7. {
    8.     public struct AudioTypeNet : NetworkMessage
    9.     {
    10.         public int content;
    11.     }
    12.     public class NetworkAudio : NetworkBehaviour
    13.     {
    14.         [SerializeField, SyncVar]
    15.         public int audioType;
    16.         public void PlayAudio() //Works
    17.         {
    18.             AudioTypeNet msg = new AudioTypeNet();
    19.             msg.content = audioType;
    20.             NetworkServer.SendToAll(msg);
    21.         }
    22.     }
    23. }
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Mirror;
    5. using Mirror.NetworkAudio;
    6. using kcp2k;
    7. public class NetworkAudioRecieveAndPlay : NetworkBehaviour
    8. {
    9.     void Start()
    10.     {
    11.         NetworkServer.RegisterHandler<AudioTypeNet>(PlayAudioWhenDetect);
    12.     }
    13.     void PlayAudioWhenDetect(NetworkConnection conn, AudioTypeNet msg)
    14.     {
    15.         if (GetComponent<NetworkAudio>().audioType == msg.content)
    16.         {
    17.             GetComponent<AudioSource>().Play();
    18.             Debug.Log("Played");
    19.         }
    20.     }
    21. }
    (The script audio may sound unexpectly sometimes, but I'll modify it later. However, it's not the problem I'm wanting solution to)
    On NetworkAudioRecieveAndPlay, you can see NetworkServer.RegisterHandler<AudioTypeNet>(PlayAudioWhenDetect), which should call PlayAudioWhenDetect(). But I've tested, it's not doing that with Debug.Log(). I've tested NetworkAudio Script with Debug.Log(), it's PlayAudio() Method works, that doesn't have a problem. The RegisterHandler() is not calling PlayAudioWhenDetect(). That's what I don't understand why:(. I've did research and this is my last result. Yet not working, so my one last hope is this Unity Forum, to not lose my months of hardworks.
    If more information is needed, you can ask me. I would open source the project, but for some asset licenses that I'm using I can't do it.
    Any help will be thanked! Thanks for your attention!
     
    Last edited: Jul 6, 2021
  2. uotsabchakma

    uotsabchakma

    Joined:
    Sep 17, 2019
    Posts:
    84
    Okay guys, I've solved my own problem.:cool: The Start Method should have:
    Code (CSharp):
    1. public class NetworkAudioRecieveAndPlay : NetworkBehaviour
    2. {
    3.     void Start()
    4.     {
    5.         NetworkClient.RegisterHandler<AudioTypeNet>(PlayAudioWhenDetect);
    6.     }
    7. }
    Don't know what happens to NetworkServer.RegisterHandler(). But NetworkClient.RegisterHandler() works!
    I'm happy to continue my development now :)
     
unityunity