Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question RegisterHandler() not calling Method, problem where?

Discussion in 'Scripting' started by uotsabchakma, Jul 3, 2021.

  1. uotsabchakma

    uotsabchakma

    Joined:
    Sep 17, 2019
    Posts:
    93
    Hello, I'm making a multiplayer game in Unity with Mirror networking system. Note that Mirror not server-client, it's with host-client system based.
    I'm trying to make my own network AudioSource as there is no free asset for Mirror in store. I might have something wrong that's not making the method PlayAudioWhenDetect(). Here's my two scripts for the system below:
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using Mirror;
    6.  
    7.  
    8. namespace Mirror.NetworkAudio
    9. {
    10.     public struct AudioTypeNet : NetworkMessage
    11.     {
    12.         public int content;
    13.     }
    14.  
    15.     public class NetworkAudio : NetworkBehaviour
    16.     {
    17.         [SerializeField, SyncVar]
    18.         public int audioType;
    19.  
    20.         public void PlayAudio() //Works
    21.         {
    22.             AudioTypeNet msg = new AudioTypeNet();
    23.             msg.content = audioType;
    24.             NetworkServer.SendToAll(msg);
    25.         }
    26.     }
    27. }
    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.  
    8. public class NetworkAudioRecieveAndPlay : NetworkBehaviour
    9. {
    10.     void Start()
    11.     {
    12.         NetworkServer.RegisterHandler<AudioTypeNet>(PlayAudioWhenDetect);
    13.     }
    14.  
    15.     void PlayAudioWhenDetect(NetworkConnection conn, AudioTypeNet msg)
    16.     {
    17.         Debug.Log("Now networkaudio received message. Time to look for if statement");
    18.         if (GetComponent<NetworkAudio>().audioType == msg.content)
    19.         {
    20.             GetComponent<AudioSource>().Play();
    21.             Debug.Log("Played");
    22.         }
    23.     }
    24. }
    25.  
    I've tested PlayAudio() of NetworkAudio class with Debug.Log(), PlayAudio() works. But the problem maybe with line 12 or 18. I've did a lot of research, but this is my final result. Yet, no errors are shown and the method isn't called. I don't know why :(.
    I've tried googling, youtube searching, documention check etc. Isn't helping. Now Unity Forum might be my last option, hope to get solution, cause otherwise I'll have to give up in this project of a lot handworks. You can ask me for more informations if these scripts can't give any solution.
    (I would open source the project, but for some asset's licenses I can't do it, sorry!)
     
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,593
    Have you tried contacting the people who made Mirror? Most Assets have some sort of customer service which you can contact. I believe they also have an official website and Discord to use to get in touch.

    I also believe some people who use Mirror frequent the Multiplayer Unity forum, but contacting the creators directly is still probably your best bet.
     
  3. uotsabchakma

    uotsabchakma

    Joined:
    Sep 17, 2019
    Posts:
    93
    Yeah, you're right! I was actually unsure too when I was writing this, about the section where I'm writing. Thanks for saying it!
    However if you're from future, if trying to get the same thing as I want, you can continue reading here - Edit: Oh no, I was in the wrong forum link!
     
    Last edited: Jul 3, 2021
  4. uotsabchakma

    uotsabchakma

    Joined:
    Sep 17, 2019
    Posts:
    93
    Oh no, am I in correct forum? No way!
     
  5. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,593
    This is an acceptable forum to have posted this, but I'm under the impression that people who use Mirror frequent the Multiplayer forum more often, so you would probably get better traffic there.
     
    uotsabchakma likes this.