UNET Cant spawn object with RegisterSpawnHandler
Hello!
I need help with spawning dynamicaly created object in the scene. Below I describe a fragment of code that is used to spawn:
public void Update()
{
if(isServer)
{
if(Input.GetKeyDown(KeyCode.Space))
{
GameObject trololo = GameObject.CreatePrimitive (PrimitiveType.Capsule);
trololo.AddComponent<NetworkIdentity>();
NetworkHash128 creatureAssetId = NetworkHash128.Parse("e2656f");
ClientScene.RegisterSpawnHandler(creatureAssetId, SpawnH, UnspawnH);
NetworkServer.Spawn(trololo, creatureAssetId);
}
}
}
public GameObject SpawnH(Vector3 position, NetworkHash128 hash)
{
Debug.Log ("lkahsgdfyagf98q23g98dq23d67qgfwd796qg29d8o7qtwr78");
GameObject trololo = GameObject.CreatePrimitive (PrimitiveType.Capsule);
return trololo;
}
public void UnspawnH(GameObject obj)
{
obj.SetActive (false);
}
In result i have error Failed to spawn server object, assetId=0000000000000e2656f netId=2 UnityEngine.Networking.NetworkIdentity:UNetStaticUpdate()
What do i wrong?
Answer by seanr · Nov 17, 2015 at 10:35 PM
you are calling "ClientScene.RegisterSpawnHandler" on the server, not on the client. it must be called on the client.
After I wrote this question, I came up with the same idea. Yes, you're right, everything works fine! Thank you! =)
Can someone clarify how this works?
First off, if you're dynamically creating objects that can be spawned, why does this have to be done on the client? Why can't the server establish and register new spawnable objects?
Secondly, if a client registers a spawnable object with RegisterSpawnHandler, how goes NetworkServer.Spawn() work? Spawn() requires a GameObject as a variable so that it knows what to spawn. How does it get this GameObject?
The signal to spawning is done within the server side code, so it needs to know what object to spawn. How can the server spawn a copy of the object that's defined by the client?
I must be missing something, but what?
I will try to write my thoughts about it.
First off, if you're dynamically creating objects that can be spawned, why does this have to be done on the client?
Only on clients side objects is spawn as GameObject, not the server. In "master-client" aka "host" (server + client) case, objects spawn only for the server-side local client and this objects interpretiert as server-side objects and it can be call commands [cmd] and other specifical server methods.
Why can't the server establish and register new spawnable objects?
Because the server do not work with GameObject, only assetId (NetworkHash128), which is associated with registrated prefabs (GameObject) on client side.
Secondly, if a client registers a spawnable object with RegisterSpawnHandler, how goes NetworkServer.Spawn() work? Spawn() requires a GameObject as a variable so that it knows what to spawn. How does it get this GameObject?
NetworkServer.Spawn(GameObject prefab) have overload method NetworkServer.Spawn(GameObject prefab, NetworkHash128 assetId), i do not know, why it does not exist in docs, because i do not understand how may works ClientScene.RegisterSpawnHandler() without this method.
Server send to client spawn message ($$anonymous$$sgType.ObjectSpawn) use NetworkServer.Spawn(GameObject prefab, NetworkHash128 assetId), client receive it and get spawn message, which contain assetId, this assetId associate with registered prefab or spawn handlers on client side and spawn object. If you seen spawnhandler delegate, you should notice that is method return GameObject, which use as a prefab. Look to the source code which implement OnObjectSpawn() method from ClientScene.cs
The signal to spawning is done within the server side code, so it needs to know what object to spawn. How can the server spawn a copy of the object that's defined by the client?
For the ClientScene.RegisterSpawnHandler() I described above. For prefab case is simply. All registered prefabs need to contain NetworkIdentity. This component have assetId property, which use to associate prefab on client side. See the source code OnObjectSpawn().
This is my imagination of how it works and I could be wrong.
Sorry for my english =)
Your answer
![](https://koobas.hobune.stream/wayback/20220612045437im_/https://answers.unity.com/themes/thub/images/avi.jpg)