- Home /
Countdown issue with Networking
Hello, at the moment I'm working on implenting Multiplayer into the Unity Car-Tutorial. The State Synchronization works so far and creating the Server and connecting to it too. But when it comes to the point, when the countdown starts and the Race is about to begin, the player, who is the Server can start, but the other Player can't start and the Countdown is going negative but very slow not in seconds.
This is an extract from my code, where I think the problem hides: [RPC] public void UpdateMenu(string menu){ menuState = menu; started = false; Debug.Log(menuState); }
public void OnGUI(){
if(networkView.isMine){
if(menuState == "Timer"){
Timer();
}
if(menuState == "InGame"){
InGame();
}
if(menuState == "Highscore"){
HighScore();
}
//GUI.Label(new Rect(100,100,100,100), pos.ToString());
}
}
public void Stop(){
foreach (MonoBehaviour script in GetComponents<MonoBehaviour>()){
script.enabled = false;
}
enabled = true;
}
public void Go(){
foreach (MonoBehaviour script in GetComponents<MonoBehaviour>()){
script.enabled = true;
}
}
public float TimeFin;
private bool started = false;
private float Countdown;
public void Timer(){
Time.timeScale = 1;
resetS = GUI.skin.label.fontSize;
GUI.skin.label.fontSize = 32;
if(!started){
TimeFin = Time.time + 5;
started = true;
}
Countdown = TimeFin - Time.time;
GUI.Label (new Rect(Screen.width/2 - 20, Screen.height/2 - 20, 40, 40),Mathf.CeilToInt(Countdown).ToString());
if(Countdown <= 0 && Network.isServer){
started=false;
//Go ();
networkView.RPC("UpdateMenu", RPCMode.All, "InGame");
}else if(Countdown <= 0 && Network.isClient){
menuState="InGame";
started = false;
}else{
Stop ();
rigidbody.velocity = Vector3.zero;
}
GUI.skin.label.fontSize = resetS;
if(count > lastCount && Network.isServer){
lastCount = count;
started = false;
networkView.RPC ("resetTimer", RPCMode.All);
}
}
[RPC] public void resetTimer (){
started = false;
}
[RPC] public void TransRounds(int r){
rounds = r;
}
public int finrounds = -1;
public void Addround(){
finrounds++;
}
public float FinTime;
public void InGame(){
if(!started){
Go ();
started = true;
}
resetS = GUI.skin.label.fontSize;
GUI.skin.label.fontSize = 32;
FinTime = Time.time - TimeFin;
GUI.Label (new Rect(Screen.width - 150, 100, 180, 50),finrounds.ToString() + " / " + rounds.ToString());
GUI.Label (new Rect(Screen.width - 150, 170, 180, 50), Mathf.FloorToInt(FinTime/60).ToString () + ":"+ (Mathf.Round((FinTime - (Mathf.FloorToInt (FinTime/60)*60))*100)/100).ToString());
GUI.Label (new Rect(Screen.width - 200, Screen.height - 100, 180, 80), Mathf.RoundToInt(rigidbody.velocity.magnitude * 1.5f).ToString()+" Km/h");
if(finrounds==rounds){
if(Network.isServer){
Finish();
}
else{
networkView.RPC ("Finish", RPCMode.Server);
}
networkView.RPC ("ReceiveScores", RPCMode.All, PlayerName, FinTime);
menuState="Highscore";
AudioListener.pause=true;
Stop ();
started = false;
}
GUI.skin.label.fontSize = resetS;
}
Sorry for the long Code,i hope you can still help me. I think it could have something to do with the timing of, when OnGUI and RPC's are called.
I would really appreciate any help Thanks :)
Are the Clients ever in the "InGame"-State, or do they never get there? Also I think you should shift most of the code into "Update()" ins$$anonymous$$d of OnGUI(). OnGUI() is called multiple times per frame, while Update() is only called once.
No the Clients actually dont reach the InGame state. When i use my code in Update i can't call GUI functions or it would be very complex to link it.
Do the RPCs actualy arrive? With the code you posted, you should see it in your Editor Log.
Yeah they do Arrive and I can see the $$anonymous$$essage in Editor Log, and it actually changes it to InGame for a short moment, but something has tp change it back, before the Ingame function is called or something like that.
Oh i forgot to mention that in l. 52
else if(Countdown <= 0 && Network.isClient){ menuState="InGame"; started = false; }
I put that in afterwards and with that it works, but then Players can start at different times, when the Server or any Player unfocuses the time, so I actually want to Server to control, when the Race is really starting, and thats, what i planned to do with changing the menuState with an RPC called by the Server.
Your answer
![](https://koobas.hobune.stream/wayback/20220613131422im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Unity networking tutorial? 6 Answers
Is server the sender of RPC? 0 Answers
How can I send RPC from one client to another client directly in multiplayer game? 0 Answers
SyncVar issue 0 Answers
When connected to a server, can other players access my static variables and/or public variables? 2 Answers