UNET - Syncing prefabs spawned by clients.

When a client fires it spawns a number of prefabs which then kill themselves shortly afterwards. They contain a number of feedback mechanics including sound and particle effects, none of which need to be synced other than when/where the prefab is instantiated.

I’ve added these prefabs to the spawnable objects list and they work on the client’s side, but not on the server. (Or they work on the server but not on the client.)

I’ve messed around with Network Identities and so forth to no avail; what is the correct way of telling the server a prefab has been spawned, where it has been spawned and which direction it is facing?

(Much of my difficulty has come from building a game in JS and the few networking tutorials all seem to be in C#. My currently functional networking for player movement is all in C# and handled in different scripts. If possible, I would appreciate help understanding how to use UNET correctly in JS, although if anybody has a C# solution to my current problem which can run as a standalone script on the client-instantiated prefab, I will be elated. I have no experience with C#, but a solution is a solution.)

The prefabs only instantiate in the game world of the client that instantiated them.

Acutally that’s how your spawning should work:

private void fire()
{
  Vector3 direction = transform.forward;
  Cmd_fire(direction)
}

[Command]
private void Cmd_fire(Vector3 direction)
{
  GameObject bullet = (GameObject) Instantiate(bullet_prefab, transform.position, Quaternion.identity);
bullet_script = bullet.getComponent<bullet_script>();

bullet_script.direction = direction;
NetworkServer.Spawn(bullet);
}

Your bullet prefab must have a NetworkIdentity component and it script should be like this:

public class bullet_script : NetworkBehaviour
{
[SyncVar] public Vector3 direction;

void Start()
{
// rotate your bullet
}

void Update()
{
// do your stuff
}
}

You should use NetworkView.RPC (c#, maybe the same for JS) to use functions on both server and client.

First add a NetworkView component to your object.
Then create an RPC function, which Instantiates your Object:

[RPC]
void MyRPCFunction( myArgs ){
   //Do Stuff, e.g. Set Player Settings
}

`

Create an RPC call, whenever you want to call the RPC function:

NetworkView MyNW=gameObject.GetComponent<NetworkView>;
MyNW.RPC("MyRPCFunction",RPCMode.All,myArgs);

`