Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by DasFloX · Jan 25, 2014 at 06:14 PM · networkingmultiplayertimerpcongui

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 :)

Comment
Add comment · Show 13
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Murkas · Jan 25, 2014 at 07:04 PM 0
Share

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.

avatar image DasFloX · Jan 25, 2014 at 08:38 PM 0
Share

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.

avatar image Murkas · Jan 25, 2014 at 08:54 PM 0
Share

Do the RPCs actualy arrive? With the code you posted, you should see it in your Editor Log.

avatar image DasFloX · Jan 25, 2014 at 09:05 PM 0
Share

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.

avatar image DasFloX · Jan 25, 2014 at 09:19 PM 0
Share

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.

Show more comments

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges