Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Resolved NetworkManager.Singleton.StartClient() failed to connect sometimes

Discussion in 'Netcode for GameObjects' started by LeeYi-user, May 11, 2024.

  1. LeeYi-user

    LeeYi-user

    Joined:
    Aug 12, 2023
    Posts:
    5
    I am using Unity Relay to make a multiplayer game. Everything works fine except that sometimes the client can't connect to the host even if there are no errors. I set NetworkManager's log level to Developer to see what happened, but it only prints these when failed:
    It didn't print [Netcode] Connected (which should have printed when successful). This only happens like 5% of the time and can be "fixed" by reconnecting (which is still kinda annoying). I once opened up 3 instances simultaneously (1 host, 2 clients), and two of them (1 host, 1 client) connected, the last one didn't. Here is the package version I am using:

    Unity 2022.3.18f1
    NGO 1.7.1
    Relay 1.0.5

    I will leave the code here for more reference.

    EDIT:

    After waiting for a while when the connection failed, it finally printed more things:
    However, even reconnecting didn't work this time. It cannot find the join code (based on this thread, I guess it is another bug).
     

    Attached Files:

    Last edited: May 11, 2024
  2. LeeYi-user

    LeeYi-user

    Joined:
    Aug 12, 2023
    Posts:
    5
    I think I found the solution. This may not be a bug, just the instability of the internet. Thanks to this post, I ended up subscribing to OnClientDisconnectCallback and switched back to the menu scene when it triggered. I may also adjust the Connect Timeout MS and Max Connect Attempts to make it switch back faster.
     
    Last edited: May 11, 2024