Make client spawn object on server.

Hi, i have a script that spawns an object when player clicks “M”.
When host clicks M ,the object spawns on every client and server.
When client clicks M, the object doesn’t spawn at all.

I’ve been doing research but i couldn’t find any correct answer. Everybody just say: Use networkserver.spawn();. It doesn’t work in my case on client; it works just on host.

My question is: How can i make client spawn an object over network?

Code:

  [Command]
    public void CmdPlaceTrap(GameObject go)
    {
        if (trapsCount != 0)
        {
            GameObject go_ = (GameObject)Instantiate(go, cursor.transform.position, this.transform.rotation);
            NetworkServer.SpawnWithClientAuthority(go_,this.gameObject);
            trapsCount--;
            RpcPlaySfx(go_);
        }
    }
    [ClientRpc]
    void RpcPlaySfx(GameObject goRPC)
    {
        AudioSource.PlayClipAtPoint(trapClipPlace, goRPC.transform.position, 3f);
    }

~ Regards

EDIT: By the way; NetworkServer.Spawn(); Doesn’t work too.

Is trapsCount a [SyncVar], or otherwise redistributed to clients?
Just speculating here, but is it possible that your if (trapsCount != 0) check doesn’t pass on clients because as far as they’re concerned, trapsCount == 0?

Is the script you referenced above on a player object, or on the traps themselves?

I have a similar setup for Instantiating a particle emitter both on the local client and spawning it on the server so other clients can see it too. My code looks something like this:

[Command]
public void CmdFunction()
{
    // Code goes here
    effect = Instantiate(effectType, point, rotation);
    NetworkServer.Spawn(effect.gameObject);
}

My code is run from the player object though, which is how the Command gets handled accordingly. If you were trying to call a command from an object without LocalPlayerAuthority, I imagine that could be causing problems too.

1 Like

Thank You! I already fixed the problem and yes it was that. Still thanks for answer.