Add Network Identity componant with code

I am having an issue where my Canvas is being disabled when I try and play a non network game. When I remove the network identity component the Canvas stays enabled, which is what I want. To solve this I am wondering if it is possible to add this component during runtime via code? But I am worried if someone selects the Network Lobby and I have the Scene to load, without a Network Identity, in the slot for “Play Scene”. I would think it would need that Network ID before it can load it?

Thanks,

-Lars

I’m not sure I understandyour premise. Why are you removing the Network ID component? If you do that, you are sure to screw with everything network that follows. I think you would be much better off if you had two lobbies: one for single-Player (non-networked) and one for multiplayer (networked).

The Problem with screwing around with the network ID component is that it contains vital Information for Unity to find corresponding objects on the other machines that connect. One bit of such Information is the AssetID, a sort of “Fingerprint” that allows Unity to identify the corresponding asset on the connecting machine. If you remove the network ID component, you may run into a lot of Problems further down the road when you start the next Scene, and the Lobby no longer can be identified on the other machines because no matching Fingerprint can be found. Although it may work with a Lobby, I don’t think it’s a good idea to add network ID by code.

Cheers,
-ch

Thanks for the reply CH!

I figured out the problem to this the other day. It turns out the NetworkIdentity is important for NetworkGames as you say (I did spend 2 days reading docs for networking before starting), but it turned out that it was not so good for non-network games. Having a NetworkIdentity on GO caused the GO to be disabled. So now I started to add the NetworkIdentity in code instead, if it is a multiplayer network game.

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

This process is working, so far.

As for the 2 Lobbies, the lobby is only being used for Multiplayer game. My scene layout is below. The map scene lets the player select what level they want to fight on first. It is only selectable by the Server / client machine. for other client the UI buttons have the intractable set to false, and that works.

Multi player Network: Welcome Scene → Lobby → Map Scene → Game play Scene
Single player or 2 player on same machine: Welcome Scene → Map Scene → Game play Scene

Thanks!