Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We are updating our Terms of Service for all Unity subscription plans, effective October 13, 2022, to create a more streamlined, user-friendly set of terms. Please review them here: unity.com/legal/terms-of-service.
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice
  4. Join us on Thursday, September 29, for a day with Unity's SRP teams here on the forum or on Reddit, and discuss topics around URP, HDRP, and the Scriptable Render Pipeline in general.
    Dismiss Notice

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:
    124
    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:
    184
    Does the script inherit from NetworkBehaviour rather than MonoBehaviour, and have a NetworkObject component?
     
  3. lavagoatGG

    lavagoatGG

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

    CosmoM

    Joined:
    Oct 31, 2015
    Posts:
    184
    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:
    58
    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:
    124
    Yes:
    1. It's inherit from NetworkBehaviour
    2. It has a NetworkObject component attached
     
  7. Max_power1965

    Max_power1965

    Joined:
    Oct 31, 2013
    Posts:
    124
    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:
    58
    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:
    124
    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:
    58
    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:
    124
    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:
    58
    I think it may be related to creating a GlobalObjectIdHash
     
unityunity