Search Unity

Question OnNetworkSpawn not called for Gameobject placed in scene

Discussion in 'Netcode for GameObjects' started by Max_power1965, Sep 17, 2022.

  1. Max_power1965

    Max_power1965

    Joined:
    Oct 31, 2013
    Posts:
    127
    I have the following code:
    Code (CSharp):
    1. public override void OnNetworkSpawn()
    2. {
    3.     base.OnNetworkSpawn();
    4.     Debug.Log("OnNetworkSpawn Called");
    5.     if (IsClient)
    6.     {
    7.         Debug.Log("I'm the client");
    8.         TransmitClientScreenSizeToServerRpc(Camera.main.ScreenToWorldPoint(new Vector3(0, Screen.height/2f, 0)).x, Camera.main.ScreenToWorldPoint(new Vector3(Screen.width/2f, 0, 0)).y);
    9.     }
    10. }
    which I placed into a gameobjct attached into a scene. The problem is that the OnNetworkSpawn is never called (in Client or the Host).
    Here is what I do to test it:
    1. Hit the play button
    2. Start the game as Host
    3. Hit the play button on the second editor (using parallel sync)
    4. Start the game as Client

    What I'm I missing?
     
  2. CosmoM

    CosmoM

    Joined:
    Oct 31, 2015
    Posts:
    204
    Does the script inherit from NetworkBehaviour rather than MonoBehaviour, and have a NetworkObject component?
     
  3. lavagoatGG

    lavagoatGG

    Joined:
    Apr 16, 2022
    Posts:
    229
    Don't you need to spawn the networkObject from script?
     
  4. CosmoM

    CosmoM

    Joined:
    Oct 31, 2015
    Posts:
    204
    Not if it's a scene object, these are automatically spawned and replicated (at least when using the NetworkManager's scene management).
     
  5. lavagoatGG

    lavagoatGG

    Joined:
    Apr 16, 2022
    Posts:
    229
    I started my game without fully reading the documentation so I need to spawn my scene NetworkObject from code. I probably did something wrong
     
  6. Max_power1965

    Max_power1965

    Joined:
    Oct 31, 2013
    Posts:
    127
    Yes:
    1. It's inherit from NetworkBehaviour
    2. It has a NetworkObject component attached
     
    g4meplayer10 likes this.
  7. Max_power1965

    Max_power1965

    Joined:
    Oct 31, 2013
    Posts:
    127
    I found the problem, the object was not added to the networkprefabs list on the Network Manager. So the checklist is the follow
    1. Add your object to the scene
    2. The object must inherit from NetworkBehaviour
    3. The object must have a NetworkObject
    4. The object must be added to the networkprefabs list on the Network Manager
     
  8. lavagoatGG

    lavagoatGG

    Joined:
    Apr 16, 2022
    Posts:
    229
    Creating In-Scene Placed Network Prefab Instances#
    In order to create a Network Prefab that can be used as an in-scene placed NetworkObject you must do the following:

    1. In the scene you wish to create the instance (or any open scene) create an empty GameObject and add a NetworkObject component to it.
    2. Add any other NetworkBehaviours required by your in-scene placed NetworkObject.
    3. Drag and drop the newly created GameObject into your Prefab (or associated) folder.
    4. Delete the GameObject instance in your scene (this is required to get a proper GlobalObjectIdHash value assigned)
    5. Finally, drag and drop an instance of your newly created Network Prefab into the scene you wish to have an instance of your in-scene placed NetworkObject.

    It doesn't say in the documetation that yoou need to add it to the networkprefabs list on the Network Manager.

    Anyway, didn't log you an error before you added the object to the networkprefabs list on the Network Manager?
     
  9. Max_power1965

    Max_power1965

    Joined:
    Oct 31, 2013
    Posts:
    127
    This is not entirely true. I didn't have to create a prefab and then deleted it from the scene, I simply created a gameobject on the scene and then drag it into the Networkprefab / NetworkObjcts list directly from the scene.
    From what I understood you need to create a prefab and then add it into the NetworkObjcts list only if you need to spawn the prefab from code, but id the Gombject is right in the scene from the beginning you can simply leave it there without the need to create a prefab.

    Anyway, this topic it's not explained in the documentation
     
  10. lavagoatGG

    lavagoatGG

    Joined:
    Apr 16, 2022
    Posts:
    229
    I think the proper way of doing this is making the scene object a prefab and not adding it to the networkPrefab list, but the way you did it also works
     
  11. Max_power1965

    Max_power1965

    Joined:
    Oct 31, 2013
    Posts:
    127
    Why do you think you need to create a prefab instead of simply placing the object in the scene?
     
  12. lavagoatGG

    lavagoatGG

    Joined:
    Apr 16, 2022
    Posts:
    229
    I think it may be related to creating a GlobalObjectIdHash
     
  13. RomanB205

    RomanB205

    Joined:
    Jun 24, 2015
    Posts:
    5
    Are you using the Editor Play mode options by any chance? I think there might be a bug where netcode requires a full domain reload in order to call every NetworkBehavior's OnNetworkSpawn for in-scene placed objects. I'll keep testing on my end but in the meantime :
    - You can call start the server, exit the game and then restart it as a server again. A bit hacky I admit, but it works.
    - Disabling Enter Play Mode Options made the calls work again consistently.