Search Unity

Resolved "OnNetworkSpawn" is triggering before "Awake"!

Discussion in 'Netcode for GameObjects' started by Mj-Kkaya, May 4, 2023.

  1. Mj-Kkaya

    Mj-Kkaya

    Joined:
    Oct 10, 2017
    Posts:
    179
    I have a issue with scene synchronization.
    Dynamic NetworkObjects behave differently in two different scenarios.

    About project:
    Unity : 2021.3.24f1
    Netcode for GameObjects : 1.4.0
    Scenes : Bootstrap, Main, Lobby, Game

    Scenario 1 : This scenario is working well.
    + Host create Lobby and Create Relay than start the "StartHost()"
    + After these action Host join "Game" scene manually.
    + Client join same Lobby, Join same Relay than start "StartClient()"
    + After these action Client join "Game" scene automatically.
    + When client join the "Game" scene, "GameplayManager"'s Awake method trigger before the "Dynamic NetworkObject"'s "OnNetworkSpawn" method.

    scenario-1.png

    Scenario 2 : Broken scenario
    + Host create Lobby and Create Relay than start the "StartHost()"
    + Client join same Lobby, Join same Relay than start "StartClient()"
    + After these action Host join "Game" scene manually.
    + After these action Client join "Game" scene automatically.
    + Bug : When client join the "Game" scene "GameplayManager"'s Awake method trigger after "Dynamic NetworkObject"'s "OnNetworkSpawn" method.

    GameplayManager is in the Game scene and "GameplayeManager-Init" log is in the its awake method!
    This error occurs when "OnNetworkSpawn" tries to reach an object in GameplayManager.

    Why is "OnNetworkSpawn" triggering at two different times in two different scenarios?
    AND
    How can I handle it this issue?

    Edit:
    + In Scenario 2 => The Host awaits the Client on the Lobby scene and then joins the Game scene with the Client.
    + This error happens only Client, not Host player.

    scenario-2.png
     
    Last edited: May 4, 2023
    trombonaut likes this.
  2. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,529
    I haven't come accross this yet but the NVar docs say something that might be related.

    https://docs-multiplayer.unity3d.com/netcode/current/basics/networkvariable

     
  3. Mj-Kkaya

    Mj-Kkaya

    Joined:
    Oct 10, 2017
    Posts:
    179
    Thanks for reply but, I have solved the issue.
    The problem is about scene synchronization.
    The host was creating the PlayerObject in theAwake method of GameplayManager.
    I moved this action to "NetworkManager.Singleton.SceneManager.OnLoadComplete" method than the problem solved!

    If you run into a problem like mine, I would recommend looking at the scene manager structure of the GalacticKittens
     
    titch_stewart and tjumma like this.