Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by nikvozelj · Dec 20, 2016 at 09:08 PM · coroutineloop

Round Loop Problem?...

I am stuck in some kind of a loop and i have no idea what is the problem. I even comented all the code step by step and i dont see why it is acting so wierd.

Code:

 LobbyMaster lm;           
 Scoreboard scoreboard;        
 public GameObject roundStats;        
 public GameObject teamStats;        
 public GameObject scoreboardGO;        
 public Text roundText;        
 public Text infoText;        
 public int numOfRounds;        
 private int round = 0;        
 private bool seeking;        

 void Start () {
     try { lm = GameObject.Find("Lobby Master").GetComponent<LobbyMaster>(); }
     catch { lm = GameObject.Find("Lobby Master_b").GetComponent<LobbyMaster>(); }
     scoreboard = GetComponent<Scoreboard>();

     NewRound();
 }

 void Update()
 {
     //for every team
     for (int i = 0; i < lm.numOfTeams; i++)
     {
         //if they have 5 points before the time ends
         if (lm.teams[i].points >= 5 && lm.curTime > 0)
         {
             if (round <= numOfRounds)
             {
                 lm.curTime = -1;
                 StartCoroutine(RoundEnd());
             }
             //last round -> end game
             else
             {
                 EndGame();
             }
         }
         //if they didnt get 5 points in time
         else if (lm.curTime == 0)
         {
             lm.curTime = -1;
             StartCoroutine(RoundEnd());
         }
     }
 }

 IEnumerator RoundEnd()
 {
     Debug.Log("Round End");
     //show round ending screen and hide sidebar
     roundStats.SetActive(true);
     scoreboardGO.SetActive(false);
     // spawn in all the teams on the result screen
     for (int i = 0; i < lm.numOfTeams; i++)
     {
         GameObject scoreGO = (GameObject)Instantiate(teamStats, new Vector2(0, 0), Quaternion.identity);
         scoreGO.transform.SetParent(roundStats.transform.FindChild("Won"));
         scoreGO.name = "Team Stats" + i;
         scoreGO.GetComponent<RectTransform>().localPosition = new Vector3(roundStats.GetComponent<RectTransform>().sizeDelta.x / 2, -60 * i);
         scoreGO.GetComponent<Text>().text = lm.teams[i].color + " team - " + lm.teams[i].points.ToString();
         scoreGO.GetComponent<Text>().color = lm.colors[i];
         yield return new WaitForSeconds(0.1f);
     }
     yield return new WaitForSeconds(2);
     //after 2 seconds destroy spawned in teams
     for (int i = 0; i < lm.numOfTeams; i++)
     {
         GameObject ts = GameObject.Find("HUD").transform.GetChild(1).GetChild(0).Find("Team Stats" + i).gameObject;
         if (ts)
         {
             Destroy(ts);
         }
     }
     //show sidebar and hide round ending screen
     roundStats.SetActive(false);
     scoreboardGO.SetActive(true);

     NewRound();
 }

 void NewRound()
 {
     Debug.Log("New Round");
     roundText.text = "Round: " + round;
     round++;
     seeking = false;
     //reset points
     for (int i = 0; i < lm.numOfTeams; i++)
     {
         lm.teams[i].points = 0;
     }
     //reset scoreboard results
     foreach (Text curText in scoreboard.results)
     {
         curText.text = "0";
     }
     ResetPlayers();
     //set random setter
     for (int i = 0; i < lm.numOfTeams; i++)
     {
         int randomSetter = Random.Range(0, lm.teams[i].players.Count);
         //lm.teams[i].players[i].GetComponent<Player>().IsSetter = true;
     }
     StartCoroutine(GameLoop());
 }

 IEnumerator GameLoop()
 {
     Debug.Log("Game Loop");
     lm.curTime = lm.setTime;
     //time for setting
     while (lm.curTime > 0)
     {
         int minutes = lm.curTime / 60;
         int seconds = lm.curTime - minutes * 60;
         lm.curTime--;
         scoreboard.timer.text = minutes + " : " + seconds;
         yield return new WaitForSeconds(1);
     }
     scoreboard.timer.text = "0 : 0";
     //when time runs out set down remaining items if they have any left
     for (int i = 0; i < lm.numOfTeams; i++)
     {
         for (int y = 0; y < lm.teams[i].players.Count; y++)
         {
             if (lm.teams[i].players[y].GetComponent<Player>().itemsLeft > 0)
             {
                 // set down remaining items
             }
         }
     }
     ResetPlayers();
     //display text
     StartCoroutine(SetInfoText("Start searching", 2f));
     seeking = true;

     //time for searching
     lm.curTime = lm.roundTime;
     while (lm.curTime > 0)
     {
         int minutes = lm.curTime / 60;
         int seconds = lm.curTime - minutes * 60;
         lm.curTime--;
         scoreboard.timer.text = minutes + " : " + seconds;
         yield return new WaitForSeconds(1);
     }
 }

 void EndGame()
 {
     //result screen
 }

 void ResetPlayers()
 {
     //move players to their teams spawn
 }

 IEnumerator SetInfoText(string text, float delay)
 {
     infoText.text = text;
     infoText.gameObject.SetActive(true);
     yield return new WaitForSeconds(delay);
     infoText.gameObject.SetActive(false);
 }

 //Update -> RoundEnd -> NewRound -> GameLoop

So basicly it does this: alt text ...Because it happens so quickly and its a lot of code at once i cant exactly tell you what is wrong D:

If you need some more explanation feel free to ask.

snipp1.png (40.6 kB)
Comment
Add comment · Show 3
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 Pengocat · Dec 20, 2016 at 11:03 PM 0
Share

Set a breakpoint and run the code line by line.

avatar image michelle12188 · Dec 21, 2016 at 01:10 AM 0
Share

Probably has something to do with you calling a loop inside update which already is getting called every frame. This means that every frame it is restarting the loop.

avatar image nikvozelj · Dec 21, 2016 at 06:59 AM 0
Share

@michelle12188 thats why i set the time to -1... I have idea i want to try when i come home...

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by nikvozelj · Dec 21, 2016 at 02:39 PM

Ok i got it... i knew it was something stupid. Round was ending after time was 0. I was using the same time for setting and searching.

Comment
Add comment · Share
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

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

88 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 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 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 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 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

How to change a variable used in a loop for the outside ? 1 Answer

Buggy While loop/Coroutine, which never truly ends? 1 Answer

What is the best way to loop with very short and accurate interval? 0 Answers

Nested coroutines 0 Answers

Coroutines Madness (); 1 Answer


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