2
\$\begingroup\$

I'm facing a problem where when I connect with the host, all the towers spawn. After I connect with a client only to find out there are no towers visible and I can walk through them when looking at the host screen.

My tower spawn script ( GameManager.cs ) :

public int numberOfDestroyedTopTowers = 0;
public int numberOfDestroyedMidTowers = 0;
public int numberOfDestroyedBotTowers = 0;

public GameObject[] topTowerSpawns;
public GameObject[] midTowerSpawns;
public GameObject[] botTowerSpawns;

public GameObject towerPrefab;

void Start() {
    CmdSpawnTowers();
}

[Command]
void CmdSpawnTowers() {

    for (int i = 0; i < midTowerSpawns.Length; i++) {
        GameObject currTower = (GameObject)Instantiate(towerPrefab);
        currTower.transform.position = midTowerSpawns[i].transform.position;
        currTower.name = "Tower [MID] " + i;
        NetworkServer.Spawn(currTower);
    }

    for (int i = 0; i < topTowerSpawns.Length; i++) {
        GameObject currTower = (GameObject)Instantiate(towerPrefab);
        currTower.transform.position = topTowerSpawns[i].transform.position;
        currTower.name = "Tower [TOP] " + i;
        NetworkServer.Spawn(currTower);
    }

    for (int i = 0; i < botTowerSpawns.Length; i++) {
        GameObject currTower = (GameObject)Instantiate(towerPrefab);
        currTower.transform.position = botTowerSpawns[i].transform.position;
        currTower.name = "Tower [BOT] " + i;
        NetworkServer.Spawn(currTower);
    }

}

The script is derived from NetworkBehaviour and it does have a Network Identity on it (Server Only checked). Also on my towerPrefab I also have a network identity with server only checked. I have added my tower prefab under the network manager "Registered Spawnable Prefabs".

\$\endgroup\$
  • \$\begingroup\$ Are you sure that you added object you want to spawn into NetworkManager spawnable prefabs? plus this object should have Network identity component. \$\endgroup\$ – Nick Dec 19 '16 at 12:12
1
\$\begingroup\$

try with a RPC call

[Command] are message from a client to the server a [ClientRPC] are messages that are send to all the clients from server

so in order to show all your clients the towers use a rpc call

void Start() {
RpcSpawnTowers();
}

[ClientRpc]    
void RpcSpawnTowers() {

    for (int i = 0; i < midTowerSpawns.Length; i++) {
        GameObject currTower = (GameObject)Instantiate(towerPrefab);
        currTower.transform.position = midTowerSpawns[i].transform.position;
        currTower.name = "Tower [MID] " + i;
        NetworkServer.Spawn(currTower);
    }

    for (int i = 0; i < topTowerSpawns.Length; i++) {
        GameObject currTower = (GameObject)Instantiate(towerPrefab);
        currTower.transform.position = topTowerSpawns[i].transform.position;
        currTower.name = "Tower [TOP] " + i;
        NetworkServer.Spawn(currTower);
    }

    for (int i = 0; i < botTowerSpawns.Length; i++) {
        GameObject currTower = (GameObject)Instantiate(towerPrefab);
        currTower.transform.position = botTowerSpawns[i].transform.position;
        currTower.name = "Tower [BOT] " + i;
        NetworkServer.Spawn(currTower);
    }

}
| improve this answer | |
\$\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.