- Home /
Why is this forloop crashing???
I've been stuck here for a day. I'm trying to call a simple return to find out which player on the network is the one performing an action. In this case, a script is highlighting units based on the returned player's alliance status when that unit is Moused-over. The most frustrating part is, SOMETIMES I crash, and other times I don't, so I know I'm doing something really dumb, but I can't figure it out.
Here's the Highlighter snippet:
void OnMouseEnter()
{
int i = 0;
tGroup = this.transform.GetComponentsInChildren<Transform>();
int tPlayer = nManager.GetTriggeringPlayer();
Debug.Log(tPlayer);
if(tPlayer!=0)
{
if(nManager.gControllers[tPlayer].alliances[uData.playerNumber]==GameController.AllianceStatus.allied)
hColor = allyColor;
if(nManager.gControllers[tPlayer].alliances[uData.playerNumber]==GameController.AllianceStatus.enemy)
hColor = enemyColor;
if(nManager.gControllers[tPlayer].alliances[uData.playerNumber]==GameController.AllianceStatus.neutral)
hColor = neutColor;
while(i<tGroup.Length)
{
if(tGroup[i].renderer)
{
tGroup[i].renderer.material.color = hColor;
i++;
}
}
}
}
and here's the GetTriggeringPlayer call:
public int GetTriggeringPlayer()
{
GameObject[] gC = GameObject.FindGameObjectsWithTag("GameController");
for(int pN = 0;pN<=gC.Length;pN++)
{
if (gC[pN].networkView.isMine)
{
int number = gC[pN].GetComponent<GameController>().PlayerNumber;
return number;
break;
}
}
int noNumber = 0;
return noNumber;
}
I'm not getting an error message, Unity will simply freeze and I have to force it to close completely; common for a loop crash.
If I remove the Highlighter script from the units or comment out the line that calls the GetTriggeringPlayer return it doesn't freeeze, but then obviously nothing is accomplished. So I know its the loop that's causing the problem.
Answer by Bunny83 · Jul 26, 2013 at 04:06 PM
That's pretty simple. You only increment "i" when the current object has a renderer component. If it doesn't you'll be stuck in your loop since I won't change anymore.
Move the i++ out of you if statement at the end of your while body.
AHH I knew it had to be something stupid. I my other solution I completely got rid of that loop, but you're right, I'm sure that was the loop that was wrecking me. Big thanks!
Answer by tw1st3d · Jul 26, 2013 at 02:57 PM
Try defining it as
public GameObject[] gC = GameObject.FindGameObjectsWithTag("GameController");
outside of your methods at the start of the class. Only thing I can think of is it's not enumerating the list fast enough and your for() is crashing because it's finding empty fields.
I tried moving this, and I still froze, but that was a good suggestion and is leading my to try some different things. I'll keep you updated.
Well I think I've solved this for now, after your suggestion got me thinking that it wasn't finding fields fast enough. I ended up setting a sort of master local player variable in my network controller, which the highlighter script is now reading ins$$anonymous$$d of calling a GetTriggeringPlayer return. For now everything looks good for the network controller, I hope it doesn't wreck local players in the future.
Your answer
![](https://koobas.hobune.stream/wayback/20220613110924im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Loop to populate array crashes Unity 1 Answer
Crash on loop using Lerp. 1 Answer
Is this loop infinite? 2 Answers
Unity crashes when doing A* pathfinding loop 1 Answer
Infinite Looping Crash 2 Answers