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

Resolved Mirror Button Button Conversion Issues in Online Scene

Discussion in 'Multiplayer' started by Simplisticated_Development, Sep 3, 2023.

  1. Simplisticated_Development

    Simplisticated_Development

    Joined:
    Jun 26, 2023
    Posts:
    61
    Hello all-

    I have been using Unity Mirror to make an online project. I am currently trying to replace the NetworkManagerHUD with my own buttons. My technique is creating a new button, adding a new OnClick function, and setting the NetworkManager object into the function. Then I make the function StartHost(), for example. Screenshot 2023-09-03 at 3.02.33 PM.png
    This works OKAY in the lobby, but there is no way to make this work in the online mode with a "Leave" button. This seems to be because when you have the NetworkManager in the DontDestroyOnLoad category, the leave button can't register the NetworkManager. The function, therefore, is not called and the button does not work. My solution was disabling the NetworkManager from being in the DontDestroyOnLoad category. This also doesn't fully solve my problem. When you run it again, you ARE transported back to the lobby when you click the leave button, but this time you get these messages if you try to reconnect: ezgif-5-05a9a3ffdf.gif
    Is it not transporting fully? Here is what my online scene's OnClick() looks like:
    Screenshot 2023-09-03 at 3.20.11 PM.png
    Here is how I set my NetworkManager:
    Screenshot 2023-09-03 at 3.23.04 PM.png
    What do I try? I am sincerely confused!

    Thanks in advance!
     

    Attached Files:

  2. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    598
    I'd avoid adding the NetworkManager directly into the OnClick callback as you won't be able to do the same in the online scene. As a simple solution have another GameObject in each scene to handle the starting and stopping of the network:

    Code (CSharp):
    1. public class OfflineSceneController : MonoBehaviour
    2. {
    3.     public void OnClickNetworkStart()
    4.     {
    5.         if (!ParrelSync.ClonesManager.IsClone())
    6.         {
    7.             NetworkManager.singleton.StartHost();
    8.         }
    9.         else
    10.         {
    11.             NetworkManager.singleton.StartClient();
    12.         }
    13.     }
    14. }
    Code (CSharp):
    1. public class OnlineSceneController : MonoBehaviour
    2. {
    3.     public void OnClickNetworkStop()
    4.     {
    5.         if (!ParrelSync.ClonesManager.IsClone())
    6.         {
    7.             NetworkManager.singleton.StopHost();
    8.         }
    9.         else
    10.         {
    11.             NetworkManager.singleton.StopClient();
    12.         }
    13.     }
    14. }
     
  3. Simplisticated_Development

    Simplisticated_Development

    Joined:
    Jun 26, 2023
    Posts:
    61
    Do I have to include the "ParrelSync" part of the script? I don't use Parrel for testing but I guess I could download it.
     
  4. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    598
    You don't have to include it, but you'll likely be using it at some point. You don't need to use it here if you're using separate buttons for host and client.
     
  5. Simplisticated_Development

    Simplisticated_Development

    Joined:
    Jun 26, 2023
    Posts:
    61

    so then how would I rewrite the script to make it work without the ParrelSync section? I am using separate Host/Client buttons...
     
  6. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    598
    There's not much to change, just create a method for each button. For example:
    Code (CSharp):
    1.     public void OnClickStartHost()
    2.     {
    3.         NetworkManager.singleton.StartHost();
    4.     }
     
  7. Simplisticated_Development

    Simplisticated_Development

    Joined:
    Jun 26, 2023
    Posts:
    61
    I tried you method. It kind of worked, but there is still a weird bug. Here is my script: Screenshot 2023-09-04 at 8.29.16 PM.png
    Here is the leave button's inspector:
    Screenshot 2023-09-04 at 8.29.05 PM.png
    Here is how the cycle runs (and fails): ezgif-3-559700e017.gif
    As you can see, when I am teleported back to the Lobby, the Host and Client buttons do not work anymore. Here are the warnings I get on clicking them:
    Screenshot 2023-09-04 at 8.30.12 PM.png
    What steps do I take next? Am I misunderstanding something? Do I need to terminate the server in my script?

    All images: (NOT IN ORDER)

    Screenshot 2023-09-04 at 8.30.12 PM.png Screenshot 2023-09-04 at 8.28.17 PM.png Screenshot 2023-09-04 at 8.29.05 PM.png Screenshot 2023-09-04 at 8.29.16 PM.png ezgif-3-559700e017.gif
     
  8. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    598
    I can't see enough to tell you, the hud suggests the server is stopping but the host's client isn't disconnecting, I don't know why that would be. Try re-adding the NetworkManager to the offline scene again to get back to its default settings.

    All you should need at this point is a button in your offline scene to StartHost and a button in your online scene to StopHost.
     
  9. Simplisticated_Development

    Simplisticated_Development

    Joined:
    Jun 26, 2023
    Posts:
    61
    You were correct. I re-made the scenes and your solution works!

    I have another question (I'm a pain, I know). How do I make my UI so that when someone joins as a host, they can only click the stop Host button, and as a client they can only click the stop client button.
     
  10. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    598
    You can use the above ParrelSync example to share the same button, that way the original project is the host and the ParrelSync clone projects are the clients. That will suffice with testing for now, you'll need more elaborate connection scenes in builds anyway.

    By the way there's some networking and Mirror video's on this channel that are worth watching: https://www.youtube.com/@ShrineApp/videos
     
  11. Simplisticated_Development

    Simplisticated_Development

    Joined:
    Jun 26, 2023
    Posts:
    61
    Cool! Thanks for your help! I think this thread is RESOLVED.
     
    cerestorm likes this.