Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved Starting Host and Client within the same OnClick method.

Discussion in 'Multiplayer' started by frankiwinnie, Jul 30, 2023.

  1. frankiwinnie

    frankiwinnie

    Joined:
    Dec 1, 2020
    Posts:
    26
    Hello everyone,

    I am trying to start the server and client within the same method that will be assigned to a button. Basically when a player loads in the scene and clicks the button, the StartHost method in the networkManager.Singleton will run if any other host has been initialized before. When the player in other session will then clicks the button the StartClient will run since the host has already been started. Could not figure out the way to achieve this behaouviour. So the same method needs to check if the host has been started if not starts the host and starts the client if host has already been started.

    Aprreciate for the help. thank you.
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,129
    You cannot automatically detect or decide for the user whether the game should start as host or client.The player actively needs to make that decision.

    Note: there is one exception which is LAN games. You can announce a host to other machines and actively scan for that but it only works in a LAN within the same subnet eg 192.168.*.* so this has very limited applicability and the amount of work to put into this is in no relation to just providing two buttons „start host“ and „join game“.
     
    frankiwinnie likes this.
  3. frankiwinnie

    frankiwinnie

    Joined:
    Dec 1, 2020
    Posts:
    26
    Thank you, I have been trying this a lot. I will then procede with two buttons as you suggested.