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: ...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.
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.
@michelle12188 thats why i set the time to -1... I have idea i want to try when i come home...
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.