Hi all,
I'm having trouble spawning dynamically created objects onto the server/client. I know that my issue is related to the registration of the "prefab" (ClientScene.RegisterPrefab
) but I can't figure out how to solve it. I've seen a couple of questions and answers on this topic, but they don't seem to help.
At the moment I've got this:
[Command]
void Cmd_SpawnAnObject()
{
GameObject spawned_object = GameObject.CreatePrimitive(PrimitiveType.Cube);
spawned_object.AddComponent<NetworkIdentity>();
spawned_object.AddComponent<NetworkTransform>();
ClientScene.RegisterPrefab(spawned_object, NetworkHash128.Parse(spawned_object.name));
NetworkServer.Spawn(spawned_object);
}
This successfully creates the object on the server, but raises a Failed to spawn server object, assetId=0000000000000000000000000000c0be netId=2
error on the client. My understanding is that calling ClientScene.RegisterPrefab
in the CMD doesn't make sense because that's running on the server, right? So it can't get to the ClientScene
.
I've tried sending the GameObject back to the client via RPC and registering that before Spawn runs but it looks like that doesn't work. E.g
[Command]
void Cmd_SpawnAnObject()
{
GameObject spawned_object = GameObject.CreatePrimitive(PrimitiveType.Cube);
spawned_object.AddComponent<NetworkIdentity>();
spawned_object.AddComponent<NetworkTransform>();
Rpc_Register(spawned_object);
NetworkServer.Spawn(spawned_object);
}
[ClientRpc]
void Rpc_Register(GameObject go)
{
ClientScene.RegisterPrefab(go, NetworkHash128.Parse(go.name));
}
I also tried creating the prefab on the client and then sending it to the command, but then I end up with the correct object on the client and none on the server. What I think would solve it is creating the prefab on the client but not an instance and then sending that "blueprint" to the server to spawn it, but I don't know if that's possible.
Everything works fine if I register these in the GUI etc, but the whole point is that I don't know what these objects are until some point during runtime (they're dynamically loaded in from another service). Am I just missing something simple?
Thanks!