2
\$\begingroup\$

I am trying to create a simple table top game with Unet. Right now I am just trying to get simple spawn mechanics working. The issue I am having is that the host computer can spawn and control objects correctly. The object gets instantiated, it gets added as a child to the armylist gameObject correctly. The client is able to see the object move around. Etc. Everything is working correctly it appears from the host side.

The client however, can only instantiate a game object then they lose all reference to it. Also when it gets instantiated it does not become a child of the correct object, it is just its own object in the scene without a parent. The host computer can see the object be spawned.

I am not sure what I am doing wrong, I have tried reading the documentation but to no avail. Any help would be greatly appreciated.

public class PlayerManager : NetworkBehaviour {

[SerializeField]
GameObject tank;
[SerializeField]
Camera PlayerCamera;

[SerializeField]
GameObject[] ArmyList = new GameObject[50];

private Vector3 placePos;
GameObject currentUnit;

void Start () {

}

void Update () {
    if (Input.GetKeyDown (KeyCode.A)) {
        Cmd_place_initial_units ();
    }
    Rpc_PlaceUnit ();
}

[Command]
void Cmd_place_initial_units(){
    currentUnit = Instantiate(tank, placePos, Quaternion.identity) as GameObject;
    currentUnit.transform.parent = transform.GetChild(0);
    ArmyList [0] = transform.GetChild (0).GetChild (0).gameObject;
    NetworkServer.SpawnWithClientAuthority (currentUnit, connectionToClient);
}

void Rpc_PlaceUnit(){
    if (ArmyList[0] != null) {
        RaycastHit hit;
        if (Physics.Raycast (PlayerCamera.ScreenPointToRay (Input.mousePosition), out hit)) {
            if (ArmyList[0].tag == "ground") {
                placePos = new Vector3 (hit.point.x, 1f, hit.point.z);
            } else {
                placePos = new Vector3 (hit.point.x, 1.2f, hit.point.z);
            }
            ArmyList[0] .transform.position = placePos;
        }
    }
}
}
\$\endgroup\$
1
\$\begingroup\$

Fixed it on my own. Sometimes walking away from the computer for a minute can really help. Here is what I was doing wrong in case anyone is interested for future reference.

The server was spawning the object correctly, but I was never sending a reference of the object back to the client. I did so by changing my function slightly and adding a new Rpc.

[Command]
void Cmd_place_initial_units(){
    currentUnit = Instantiate(tank, placePos, Quaternion.identity) as GameObject;
    NetworkServer.SpawnWithClientAuthority (currentUnit, connectionToClient);
    RpcSyncUnits (currentUnit);
}

[ClientRpc]
void RpcSyncUnits(GameObject x){
    currentUnit = x;
    currentUnit.transform.parent = transform.GetChild(0);
    ArmyList [0] = transform.GetChild (0).GetChild (0).gameObject;
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.