Question by
SovietBear · Aug 13, 2016 at 09:14 PM ·
networkingmultiplayervariablesintinteger
Sync Var not syncing on client.
For whatever reason, the integer multiscene is successfully being set on the server, but client side it doesn't change when connecting to a server. Here is the script:
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using UnityEngine.Networking.Types;
public class NManage: NetworkBehaviour {
[SerializeField]
GameObject[] spawners;
public List<GameObject> allPlayers = new List<GameObject>();
public GameObject localPlayer;
public networkSetup NS;
public NetworkManager NM;
public variableKeeper VK;
public int maxPlayers = 10;
[SyncVar(hook="setScene")]
public int multiScene = 0;
// Use this for initialization
void Start () {
VK = gameObject.GetComponentInParent<variableKeeper> ();
NM = gameObject.GetComponentInParent<NetworkManager> ();
}
// Update is called once per frame
void Update () {
setScene(SceneManager.GetSceneByName (VK.loadSC).buildIndex);
if (localPlayer == null) {
localPlayer = GameObject.FindGameObjectWithTag ("localPlayer");
} else if (NS == null) {
NS = localPlayer.GetComponent<networkSetup> ();
} else if (NS.isServer) {
if (spawners.Length <= 0) {
spawners = GameObject.FindGameObjectsWithTag ("Spawner");
} else if (spawners [0].activeSelf != true) {
for (int i = 0; i < spawners.Length; i++) {
spawners [i].SetActive (true);
}
}
if (localPlayer != null && !allPlayers.Contains (localPlayer)) {
allPlayers.Add (localPlayer);
}
if (allPlayers.Count < GameObject.FindGameObjectsWithTag ("remotePlayer").Length + 1) {
for (int x = 0; x < GameObject.FindGameObjectsWithTag ("remotePlayer").Length; x++) {
if (!allPlayers.Contains (GameObject.FindGameObjectsWithTag ("remotePlayer") [x])) {
allPlayers.Add (GameObject.FindGameObjectsWithTag ("remotePlayer") [x]);
}
}
} else if (allPlayers.Count > GameObject.FindGameObjectsWithTag ("remotePlayer").Length + 1) {
for (int x = 0; x < allPlayers.Count; x++) {
if (allPlayers [x] == null) {
allPlayers.RemoveAt (x);
}
}
}
}
NM.maxConnections = 10;
}
public void joinGame(string ip, int port){
NM.networkAddress = ip;
NM.networkPort = port;
NM.StartClient ();
//SceneManager.LoadScene (SceneManager.GetSceneAt(multiScene).name, LoadSceneMode.Additive);
}
public void setScene(int sceneNum) {
if(multiScene != sceneNum && isServer) {
multiScene = sceneNum;
}
}
}
Comment