MonoBehaviour vs. NetworkBehaviour

Hello, So I finished my single player game and I am adding multiplayer to it. As I am adding network code such a [syncVars] and such it is asking me to use NetworkBehavior instead of the
MonoBehaviour.

Is this still safe for games that will be non network games or will I run in to problems?

You will run into problems. Unfortunately the Unet HLAPI does not have a built in single player mode, and objects with a NetworkIdentity component will not be activate if the server or client isn’t up. One work around is to just start the server anyway when running a single player game. You could try binding the server to the loopback address which should prevent outside clients from connecting (127.0.0.1 is the loopback address). I haven’t tried this work around, so can’t say for sure if you’ll run into further problems.

My understanding is Photon has a built in single player mode. You might consider looking into that.

Joe, thanks for your constant help here as I learn. So my work around so far has been this

In SceneAScript, I have a script that has a static bool.

public static bool isNetworkGame = false;

If the player clicks on the multiplayer button it sets it to true and loads the LobbyManager, I have a DoNotDestroy(); on SceneAScript GO. When the Lobby starts a game I run this code on SceneBScript GO.

if (SceneAScript.isNetworkGame){
    gameObject.AddComponent<NetworkIdentity>();
}

So my game structure is like this for my scenes.

  1. Scene 1 welcome to game pick multiplayer or single player - picks multiplayer.
  2. LobbyManager the other player joins and Scene 2 loads.
  3. Scene 2 is a map room with UI buttons to select a level (scene to load and play). Only the Server can pick buttons, buttons for clients are disabled(and it works correctly). - Server makes a map selection and information is sent to clients to load.
  4. Scene 3 the game level that is played.

I’ve never used LobbyManager, so I’m just guessing how I would do it. Maybe someone else can provide better guidance who has real experience with it. What I would investigate is clicking single player loads scene 2 directly instead of the LobbyManager stuff. The player selects their scene, and skips all the sending to clients stuff obviously. Then loads scene 3. When in “single player” mode, scene 3 sets NetworkManager.serverBindAddress to “127.0.0.1” and NetworkManager.serverBindToIP to true, then calls NetworkManager.StartHost. This should start networking in host mode (so both server and client) and only allow other clients running on the same local computer that attempt to connect to 127.0.0.1 to connect to the server.

In a multiplayer game you would want to avoid setting serverBindAddress and serverBindToIP as above.

This is just the way I’d think I’d get it to work, but again I am not speaking as someone who has actually done this.

https://docs.unity3d.com/ScriptReference/Networking.NetworkManager-serverBindAddress.html
https://docs.unity3d.com/ScriptReference/Networking.NetworkManager-serverBindToIP.html
https://docs.unity3d.com/ScriptReference/Networking.NetworkManager.StartHost.html

I will look at this links and reread your idea. Thanks Joe!