Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Resolved NetworkObject.spawn() does not work !

Discussion in 'Netcode for GameObjects' started by MariusBaud, Sep 10, 2023.

  1. MariusBaud

    MariusBaud

    Joined:
    Jun 1, 2022
    Posts:
    8
    NetworkObject.spawn() does not work ! I'm using unity netcode for gameObjects and I try to spawn a gameObjects by instantiating it with the host and then use getComponent().spawn() to instantiate it on the client but that does not work ! The gameObject does not spawn on the client but when I Do "print(NetworkObject.isSpawned)" (on the host) it is returning true. the prefab I instantiate on the host has a networkObject component and is in the network prefab list of the networkManager and the attached script is a networkBeahviour.

    I try to find the answer to my question on forum but all of them say that getComponent().spawn() is the way to do. I want to do things like that because i want to sync the transform of the object on client using NetworkTransform component. I'm also using unity relay for transport and Unity lobby ( maybe that could be the problem I don't know ) Thanks for the help
    Code (CSharp):
    1. GameObject newRock = Instantiate(RockPrefab, transform);
    2. if (GameManager.instance.IsPlayingMultiplayer)
    3. {
    4.    newRock.GetComponent<NetworkObject>().Spawn(true);
    5. }
     
    ANLevant likes this.
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,467
    That condition is likely to be incorrect. It needs to check if the local instance is either host or server, not whether multiplayer is currently commencing. I'm guessing this causes Spawn to be run for both host and client but for the client this will log as an exception because only the server/host can spawn network objects.

    Try to confirm that by having the Console open for both host and client.

    In case you weren't aware, you can use ParrelSync to avoid having to make builds all the time and be able to monitor both host and client in the Unity editor.
     
    MariusBaud likes this.
  3. MariusBaud

    MariusBaud

    Joined:
    Jun 1, 2022
    Posts:
    8
    Thanks for your answer but this code only run on the host
    Code (CSharp):
    1. if(IsHost)
    2. {
    3.       InstantiateRock();
    4. }
    5.  
    6. void InstantiateRock()
    7. {
    8.         GameObject newRock = Instantiate(RockPrefab, transform);
    9.         if (GameManager.instance.IsPlayingMultiplayer)
    10.         {           newRock.GetComponent<NetworkObject().Spawn(true);
    11.         }
    I already using ParrelSync to test :)
    when I start the game and spawn the prefab on the host side, nothing happen on the client side and nothing appears in the console

    Does this code need to be call on the object owned by the player ?
     
    Last edited: Sep 10, 2023
  4. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,467
    Did you check that Spawn gets called? IsPlayingMultiplayer may be false.
     
    MariusBaud likes this.
  5. MariusBaud

    MariusBaud

    Joined:
    Jun 1, 2022
    Posts:
    8
    Yes IsPlayingMultiplayer is true and when I call print(NetworkObject.isSpawned) that print true.
    When I began coding the multiplayer for my game, I used this method and that was working, then I stop using it and I added unity relay and Unity lobby for the multiplayer, do you think this could be causing this problem?
     
  6. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,467
    If you can quickly test it locally without using the relay/lobby services I would give that a shot.
    If the spawn happens immediately after launching the game respectively connecting, this could also be a timing issue. Try hooking up the spawn to a keyboard action to see if it'll work at any later time.
     
    MariusBaud likes this.
  7. MariusBaud

    MariusBaud

    Joined:
    Jun 1, 2022
    Posts:
    8

    I tested without using relay/lobby and there is the issues ( the obj don't spawn on the client side and on the host side that print true for print(NetworkObject.isSpawned) ).
    I also tried to hooking up the spawn to a keyboard action but that doesn't resolve the issue.
     
  8. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,467
    Hmm maybe try making a simple barebones example for spawning.
    Could be something that's not easy to spot, perhaps the prefab isn't actually in the list because the RockPrefab assignment references a different prefab or something like that.
     
    MariusBaud likes this.
  9. MariusBaud

    MariusBaud

    Joined:
    Jun 1, 2022
    Posts:
    8
    I solved the problem !!! I just needed to set the "Spawn with observer" of the networkObject to true, this is a very silly error I've been on for over a day :(, thanks for taking your time to help me out :).
     
    CodeSmile likes this.