Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Bug The dynamically spawned network object in Host is not spawned(appeared) in Client.

Discussion in 'Netcode for GameObjects' started by sava-game, Sep 13, 2023.

  1. sava-game


    Feb 14, 2023
    I am working on PvP for a 2D game.
    All things are working on Unity Editor which means that 2 spawned network objects in Host are also spawned in Client.
    But it is not working in APK build in Android Device.
    I attached some code block and video.

    --- Spawn object in Host(only in Host)

    public PvPCruiser CreateCruiser(PvPCruiser cruiserPrefab, ulong ClientNetworkId, float x)
    PvPCruiser cruiser = Object.Instantiate(cruiserPrefab, new Vector3(x, 0f, 0f), Quaternion.identity);
    cruiser.GetComponent<NetworkObject>().DontDestroyWithOwner = false;
    return cruiser;

    So, the object is spawned in Host and it is reflected in Client when I play it on Unity Editor

    But it is not working well in android build in Android Device.
    What i mean : the object is only spawned in Host , it is not reflected in client in Android Device.

    I will attach 2 videos to show them.(Left is Host, Right is Client in videos) (Unity Editor) (Android Emulator)

    I want to find a clue to resolve it as soon as possible.
  2. casalepeter


    Sep 25, 2018
    Further observations from me, a team member of OP:

    First of all, it seems that the problem is not just with object instantiation but rather with the network synchronization on Android builds specifically. When running the game in the Unity Editor, network communication works flawlessly: objects are spawned on both the Host and Client sides and can interact with each other without any hiccups.

    1. Client-Host Communication: During my debugging, I found out that the ClientNetworkId is received correctly by the Host but is not reflected on the Client side when running on an Android device.

    2. Logcat: I went through the Android Logcat and found no specific errors related to networking or object instantiation, which makes this all the more puzzling.

    3. Debugging: Further debug logs indicated that the CreateCruiser function is being called and executed without any errors on the Android build; however, the spawned object simply does not appear on the Client side.

    4. Network Permissions: I double-checked my AndroidManifest.xml, and all the required permissions for networking are set.
    Additional Clues:
    I tried bypassing the SpawnWithOwnership(ClientNetworkId) function and instead used a basic Spawn() function for testing. The object was spawned on both the Host and Client when running on an Android device, but obviously, I couldn't control it properly due to lack of ownership.

    This leads me to believe that the issue might be tied to the ownership settings or the SpawnWithOwnership() function specifically when running on Android.

    I hope these additional observations help us zero in on the actual issue. If anyone else has encountered this problem and found a solution, your insights would be greatly appreciated.
  3. Znimu


    May 24, 2015
    I have the same issue and I think that SpawnWithOwnership doesn't work at all for clients, since several versions of Unity.
    I tried with a windows build and in the editor. The object just doesn't appear, while a simple Spawn works fine.
  4. Znimu


    May 24, 2015
    I was not very accurate. It doesn't work when the owner is a client, not the host.
  5. sava-game


    Feb 14, 2023
    public async Task AddCapacity(int capacityToAdd)
    for (int i = 0; i < capacityToAdd; ++i)
    var item = await _itemFactory.CreateItem();
    await Task.Delay(100);

    as you can see above codes, I add some delay to creating and spawning next networkobject to resolve the issue.
  6. Znimu


    May 24, 2015
    Oh, thanks for sharing this.
    I don't know if it is very safe though. If a player PC is slow or something.
  7. Unity_Brandon


    Unity Technologies

    Sep 29, 2016
    The code seems fairly straightforward, instantiating a PvPBuildableWrapper object and setting it up for network synchronization with SpawnWithOwnership(clientID). However, the fact that the issue only appears on Android and not within the Unity Editor suggests that there may be a platform-specific issue or behavior that's causing the problem.

    Here are a few things you might want to consider checking:
    1. Type Generics: Unity sometimes behaves differently with generic types on different platforms. Although not very likely, you might want to try using a non-generic version for testing purposes.
    2. Object Activation: buildableWrapper.gameObject.SetActive(true); is called after SpawnWithOwnership. Sometimes, the order of operations matters. Consider setting it active before spawning with ownership to see if that has any effect.
    3. Network Initialization: Ensure that the NetworkObject component is fully initialized before calling SpawnWithOwnership(clientID). Initialization delays could lead to unexpected behavior.
    4. Logs and Debugging: Add more logging around the SpawnWithOwnership(clientID) call to ensure that clientID is what you expect and that the function is being called when and where you expect it.
    5. Unity Version: Sometimes, these types of issues are Unity version-dependent. Make sure that you and your client are using versions of Unity and NGO that are known to be compatible.
    6. Build Settings: Double-check if there are any build settings that might be affecting the Android build differently from the Editor build.
    Here's a slightly modified version of the function, with added logging for debugging purposes:

    Code (CSharp):
    1. private TBuildable CreateBuildingBuildable<TBuildable>(
    2.     PvPBuildableWrapper<TBuildable> buildableWrapperPrefab,
    3.     IPvPFactoryProvider factoryProvider,
    4.     ulong clientID) where TBuildable : class, IPvPBuilding
    5. {
    6.     PvPHelper.AssertIsNotNull(buildableWrapperPrefab, factoryProvider);
    8.     // Debug log to check values
    9.     Debug.Log($"ClientID: {clientID}");
    11.     PvPBuildableWrapper<TBuildable> buildableWrapper = Object.Instantiate(buildableWrapperPrefab);
    13.     buildableWrapper.gameObject.SetActive(true);  // Setting active before spawn
    15.     // More debug logging
    16.     Debug.Log("About to spawn with ownership...");
    18.     buildableWrapper.GetComponent<NetworkObject>().SpawnWithOwnership(clientID);
    20.     // Debug log to confirm spawn
    21.     Debug.Log("Spawned with ownership");
    23.     buildableWrapper.GetComponent<NetworkObject>().DontDestroyWithOwner = false;
    24.     buildableWrapper.StaticInitialise(_commonStrings);
    25.     buildableWrapper.Buildable.Initialise(factoryProvider);
    27.     return buildableWrapper.Buildable;
    28. }

    Please try these and let me know if they resolve your issue or provide any additional context as to what the issue may be.
  8. NoelStephens_Unity


    Unity Technologies

    Feb 12, 2022
    To add to what Brandon just posted yesterday:

    Deferred Message (SpawnTimeout) Period:
    With Android and/or any platform that could take a bit longer to load assets, you could be running into an issue with messages being received but not processed for a "yet to be instantiated/spawned" object. The default setting for deferred messages (i.e. messages received for a NetworkObject->NetworkBehaviour yet to be spawned/instantiated) is 1 second...if the associated NetworkObject (or NetworkBehaviour component) does not exist within that period of time then the message gets dropped.

    You might try increasing the NetworkManager.NetworkConfig.SpawnTimeout value to like say 10 seconds (or some value you think is reasonable) and see if that helps.

    Some other pointers:
    • When things seem to not work on mobile devices (especially Android), as a "sanity" check you might make a build with the "Project Settings->Player->Other Settings->Optimization->Managed Stripping Level" set to minimal to just make sure some critical method or the like is not being stripped away by accident (if you run into this issue you will want to read about preserving code using annotations)
    • Disabled NetworkBehaviours will not be considered "associated" with a NetworkObject if they are disabled when the NetworkObject is spawned (you can disable them after the NetworkObject is spawned as the NetworkObject will have already registered it...RPCs will still get invoked and NetworkVariables will still be updated if a NetworkBehaviour is disabled post-spawn).
    Let me know if any of the above helps?