Why no StartHost()? An answer

TL;DR:
UnityEngine.Networking.NetworkManager.StartHost() IS publicly exposed, but is “ineligible” for use in the UI Button’s OnClick inspector element because the methods must be:

  • public,
  • have zero or one parameters, and
  • a return type of null.

UnityEngine.Networking.NetworkManager.StartHost() returns “UnityEngine.Networking.NetworkClient”, and so does not show up in the inspector.


At around 3:02, Ben says “for some they haven’t publicly exposed the StartHost Method.”
The video is old, but this may explain the reason why it isn’t currently exposed:

The button’s OnClick inspector element starts getting explained at around 8:16, and the “eligibility” for a method to be selected starts getting explained explained at around 10:30.

To be eligible to show up in the inspector, the method must be public, with zero or one parameters and a return type of void.

The documentation of UnityEngine.Networking.NetworkManager.StartHost() shows a return type of “UnityEngine.Networking.NetworkClient”, which means the method cannot show up.
https://docs.unity3d.com/ScriptReference/Networking.NetworkManager.StartHost.html

This, of course, can pose a problem of what to do with the NetworkClient returned from StartHost() (without returning the Network Client, it’s lost to the Garbage Collector); I just have it sitting in a public static variable until I know what to do with it.

Hope this helps somebody :slight_smile:

3 Likes

Privacy & Terms