Question by
ghostravenstorm · Apr 27, 2017 at 06:22 PM ·
networkingsynchronizationsingleton
Game Manager singleton doesn't sync across the network
I have a Game Manager singleton that doesn't appear to be working at all.
I'm trying to spawn a timer object in my singleton script but I get this error.
My variable for my timer object isn't being set.
public class GameManager : NetworkBehaviour{
public static GameManager singleton;
public GameObject countdownTimerPrefab;
public Text uiText;
[SyncVar] public GameObject roundTimer;
void Awake(){
if(singleton == null){
singleton = this;
DontDestroyOnLoad(gameObject);
}
else{
Destroy(gameObject);
}
}
void Start(){
CmdInitializeTimer();
}
[Command]
private void CmdInitializeTimer(){
this.roundTimer = GameObject.Instantiate(countdownTimerPrefab);
NetworkServer.Spawn(roundTimer);
this.roundTimer.GetComponent<CountdownTimer>().Initialize(3, 0, this.uiText, this.OnTimerExpired);
this.roundTimer.GetComponent<CountdownTimer>().StartTimer();
}
public void OnTimerExpired(){
}
Comment
Answer by ghostravenstorm · Apr 27, 2017 at 07:44 PM
I made a different script on a different game object that exists with the scene like my singleton only this object's script is not a singleton and my timer instance works as intended. I don't know why. I just don't know what's up with networked singletons.
public class TimerManager : NetworkBehaviour{
public Text uiText;
public GameObject countdownTimerPrefab;
public GameObject roundTimer;
private void Start(){
if(isServer){
this.roundTimer = GameObject.Instantiate(countdownTimerPrefab);
NetworkServer.Spawn(this.roundTimer);
this.roundTimer.GetComponent<CountdownTimer>().Initialize(0, 10, this.uiText, this.OnTimerExpired);
this.roundTimer.GetComponent<CountdownTimer>().StartTimer();
}
}
private void OnTimerExpired(){
Debug.Log("Timer Expired");
// End game event.
// Get highest scored earned.
}
}
Your answer
Follow this Question
Related Questions
UI Text synchronize with UNET 1 Answer
Unet - spawning bullets, two issues 1 Answer
Unity UNet Correct Usage, Unity 5.4 Beta 0 Answers
Sync Mesh shape over network 1 Answer