- Home /
Network.Instantiate a non-prefab?
I'm procedurally generating some spaceships which the players control. How can I send these objects over the network as Network.Instantiate keeps saying I have to spawn a prefab assigned within the editor.
Answer by Tomer-Barkan · May 25, 2013 at 07:31 PM
The way Network.Instantiate works, is that it sends the identifier of the prefab to all the clients, and not the prefab itself. So this means that the prefab must exist in all the clients, and not just on the server.
Hence, you are only able to instantiate prefabs with Network.Instantiate, you cannot instantiate procedurally generated objects.
Read about it in the documentation: http://docs.unity3d.com/Documentation/Components/net-NetworkInstantiate.html
And read here about alternatives to instantiate a non-prefab: http://answers.unity3d.com/questions/163493/networkinstantiate-a-non-prefab.html
And here: http://docs.unity3d.com/Documentation/Components/net-RPCDetails.html
Yeah, I saw that alternative, didn't help me much.
The RPC call though. If I'm right in understanding this, I could call the same function on every machine, using some kind of seed to generate the same ship on each machine?
Atm I just use random.range and all that basic stuff, how would I link it up to a seed?
Yes, exactly. Calculate the random values on the server, and then serialize them to a string to send to the others via the RPC call.
Another thing you might try (haven't tried myself) is to generate a random seed, and then send it to all clients. Then set them all to the same seed with "Random.seed", and they should generate the same values in the next random calls.
Yeap, I guess that's the best way. Thanks for your help. :P