Unity UNET- Can't spawn object with client authority

795 views Asked by At

I have a problem where I can't spawn the selected object from clients. It works perfectly when the action is performed by the host, but not when a client attempts it. When a client attempts it, I get the following error: "SpawnWithClientAuthority player object is not a player". This is quite confusing as it works perfectly when it's performed by the host.

The code for this particular part is the following:

 private void updateAppearance(GameObject newObject)
{

    Destroy(appearance);

    hiderModel.SetActive(false);

    int newObjectNum = propNames.IndexOf(newObject.name);
    activePropIndex = newObjectNum;
    Debug.Log(newObjectNum);


    newObject = (GameObject)Instantiate(props[newObjectNum], playerCam.gameObject.transform);
    newObject.transform.localPosition = new Vector3(0, getObjectHeight(newObjectNum), 0);

    NetworkServer.SpawnWithClientAuthority(newObject, gameObject); <--- This part gives the error

    appearance = newObject;
    appearance.transform.localPosition = new Vector3(0, appearance.transform.localPosition.y, 0);


}

The object to spawn has localAuthority set and has a network transform on it. The object is registered as a spawnable object and it is the instantiated prefab that I am passing to the SpawnWithClientAuthority method. As far as I have read, this should allow the function to work, but unfortunately it doesn't.

Any ideas on how to fix this?

Thanks in advance

1

There are 1 answers

0
Sergey  Molchanovsky On

It works perfectly when the action is performed by the host, but not when a client attempts it

Because ONLY SERVER can spawn objects across the network. Wrap your code to [Command] to execute it on server.