Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

SpawnObjects() not working

Discussion in 'Multiplayer' started by PieterAlbers, Sep 2, 2015.

  1. PieterAlbers

    PieterAlbers

    Joined:
    Dec 2, 2014
    Posts:
    236
    Hi all,

    I am updating one of our existing games to feature (basic) multiplayer. Besides some basic experimenting multiplayer is pretty much new to me.

    Nevertheless, I am making progress and everything is setup in such a way users can join a game, run, jump and move around. Now for the next step.

    In our scene we dynamically load prefabs for the world; floors, buildings, misc. (this is the same for each user, they all play the same level).The buildings need to be destroyed.For now I only want to sync their health.

    When they are loaded into the scene they get assigned a DestructibleBuilding component which is now a NetworkBehavior. When running as a host everything shows up as expected. The client, however, has none of the buildings visible. They are all disabled. I've been reading up on NetworkServer.SpawnObjects() but it is not working. Even when I manually call it after the scene/level setup is done.

    My guess is that it can't work since I am loading in the world stuff dynamically. Is this correct?
    I am bit unsure on how to approach this. Anyone has any tips or advice?

    Thanks in advance!
    Pieter
     
  2. TehGM

    TehGM

    Joined:
    Nov 15, 2013
    Posts:
    89
    Not sure if you're correct, but I had similar issue. I had a NetworkBehaviour game manager, and it was getting spawned on the host only. Even SpawnObjects() didn't help. I simply converted to MonoBehaviour and used NetworkMessages, as I didn't need anything more yet.
    However, I never said anything about this issue as I thought it's problem with just my implementation.
     
  3. PieterAlbers

    PieterAlbers

    Joined:
    Dec 2, 2014
    Posts:
    236
    Thanks for your reply! I am not sure if we experience similar issues. I have a NetworkBehavior manager as well and I can spawn players through it.

    I can't seem to be able to 'sync'/spawn the dynamically loaded level objects.
     
  4. bartm4n

    bartm4n

    Joined:
    Jun 20, 2013
    Posts:
    57
    Even though you are spawning things dynamically, you need to make sure that the prefabs for the things to be spawned are assigned in your NetworkManager under Registered Spawnable Prefabs.
     
  5. PieterAlbers

    PieterAlbers

    Joined:
    Dec 2, 2014
    Posts:
    236
    So I am getting more confused by the minute. I understand that objects need to be registered, but even then it is not working. I have been testing and trying for a while now but nothing seems to work.

    Here are some of my findings and issues

    1. If objects have NetworkBehavior (and NetworkIdentity) they will show up (but not sync) if you start a host in the editor and a client as standalone. When you do it the other way around (standalone as host, editor client) none of those objects show (they are disabled) in the editor.

    2. When I add the prefabs to the Registered Spawnable Prefabs different prefabs are shown on the client.
    To explain more clearly. I load and instantiate an object with a lot of children (buildings). 5 of those already have a NetworkIdentity. These buildings are already registered in the NetworkManager. On the host everything looks fine. However on the client those 5 buildings are replaced by 5 other buildings. It seems that the first 5 children of my main object are spawned on the location of the 5 buildings I want to sync. Those buildings (the ones I want) are again inactive. Nothing is synced btw, not even the new incorrect ones.

    3. I tried many combinations of manually registering prefabs, then calling either Spawn() or SpawnObjects(). THe result is either inactive prefabs or the different building issue as above.

    4. The only way that I got it to work (more or less) is to already prepare everything in a scene. So we would make a completely finished scene or level. Which, unfortunately, is pretty much out of the question. We use so many prefabs across almost 40 levels that this would be the most inflexible way of developing this game.

    What I would want is that the host dynamically, when setting up the level, registers the spawnable prefabs. Then the client as well (is this necessary?). Then when the client is added/done SpawnObjects is called and they are synced.

    Is this how it should work?
     
  6. bartm4n

    bartm4n

    Joined:
    Jun 20, 2013
    Posts:
    57
    I think that you have the right general idea, but I suspect that it has something to do with the way that you are dynamically creating your prefabs. The fact that you are getting different behaviors and objects spawned on each end supports that notion.

    As a stab in the dark, it sounds like some of the prefab pieces are correctly registered for spawning on both client/server and others aren't. I would start by making your prefabs simpler with all component objects statically registered at build time to see if you still have issues.

    Also, you certainly can register spawnable prefabs dynamically - information about this is included in the UNET documentation. It is in the first block of code in the section about spawning objects: http://docs.unity3d.com/Manual/UNetSpawning.html.

    If you haven't thoroughly read through this documentation, that is a good starting place.