- Home /
Change player object - UNET
Hi guys I'm trying to change the player's model in my multiplayer game (UNET). But I can't seem to get the new object to actually spawn for the client.
The problem is that 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.
The above method was the closest I could get to the actual solution. I tried changing it to a method where I'm using the [command] thingy, but with it I'm not even able to perform it by the host, since for some reason I'm not getting the correct playerid. The code for this method is the following:
[Command]
public void CmdUpdateAppearance(int playerID, GameObject newObject, GameObject playerCam)
{
Debug.Log("Change please");
GameObject.Find("NetworkManager").GetComponent<Teams>().hiders.CopyTo(hiders);
Debug.Log(playerID);
GameObject player = hiders.ElementAt(playerID);
hiderBehaviour hb = player.GetComponent<hiderBehaviour>();
NetworkServer.Destroy(player.GetComponent<hiderBehaviour>().appearance);
hb.hiderModel.SetActive(false);
int newObjectNum = hb.propNames.IndexOf(newObject.name);
hb.activePropIndex = newObjectNum;
newObject = (GameObject)Instantiate(props[newObjectNum], playerCam.gameObject.transform);
newObject.transform.localPosition = new Vector3(0, hb.getObjectHeight(newObjectNum), 0);
NetworkServer.SpawnWithClientAuthority(newObject, player);
hb.appearance = newObject;
hb.appearance.transform.localPosition = new Vector3(0, hb.appearance.transform.localPosition.y, 0);
}
I'm still not sure exactly why I'm not getting the correct playerID, but that method seems to be further away from actually being correct. There are quite a lot of objects which can be spawned through this so I can't have all of them instantiated and simply switching between them as it would most likely be horrific for performance.
Any ideas on how I can actually do this? Or maybe change to first method to work from clients as well?
This is kind of urgent so any help would be appreciated
Answer by GokusGString · Oct 09, 2017 at 09:24 PM
Got around it by using commands and client rocks and instantiating the object in the clientrpc