Search Unity

Failed to spawn server object

Discussion in 'Multiplayer' started by GuruGurra, Sep 3, 2020.

  1. GuruGurra

    GuruGurra

    Joined:
    Jan 18, 2018
    Posts:
    38
    Using Mirror I register a prefab like:

    ClientScene.RegisterPrefab(MyPrefab, SpawnMyPrefab, UnspawnMyPrefab);

    and on the server spawn like:

    GameObject myInstance = Instantiate(MyPrefab);
    NetworkServer.Spawn(myInstance);

    • When a client connect I can see that SpawnMyPrefab is called on the client and everything works as expected.
    • When the client disconnect I can see that the spawned objects on the client are removed (but UnspawnMyPrefab is never called).
    • When the client then re-connect, SpawnMyPrefab is never called, instead I get a logged error message saying:
    Failed to spawn server object, did you forget to add it to the NetworkManager?

    What am I doing wrong?
     
  2. qiqiqi

    qiqiqi

    Joined:
    Nov 3, 2015
    Posts:
    17
    from the bold message it look like somehow you did not successfully register the prefab,
    - make sure you really have called it,
    - and if you don't need custom spawn handler, dont use this overload version, use the simplest RegisterPrefab(GameObject prefab)
    - I remember if you registered custom spawn handler, you should pass an assetID when calling NetworkServer.Spawn, this also a possible reason

    and I think there might be other error or warnning messages before the bold one
     
    Last edited: Sep 3, 2020
  3. GuruGurra

    GuruGurra

    Joined:
    Jan 18, 2018
    Posts:
    38
    I must have successfully registered the prefab otherwise it wouldn't have spawned the first time the client connect, it's only at the second connection that it isn't registered. (...does the registration disappear when I disconnect?)
    I need the object to spawn on a different location on the client than on the server, so I need the custom spawn handler.
     
  4. GuruGurra

    GuruGurra

    Joined:
    Jan 18, 2018
    Posts:
    38
    By some reason I must re-register the prefab every time I open a new connection. Then it works.