- Home /
Script Freezing Unity
So I was working on a bit of coding, and everything else is working fine, but this one bit of code seems to be causing unity some serious problems, and I don't know why. I am using the newest unity build, 5.0.2f1 . It's not even outputting debug.logs (which I removed from here to make it cleaner), it's just eating up more and more ram as it tries to run. the script is called by pressing a GUI button, and here it is. Before you ask, I've done many checks and variations to make sure I'm not doing something stupid like getting caught in an infinite loop. All the values are greater than what I'm trying to subtract from them. To maybe narrow it down, when running the script, if power < OtherPower, it works fine, without freezing.
void Battle()
{
if (power > OtherPower){
damage = power - OtherPower;
OtherPower = OtherPower * System.Math.PI;
}
if (power < OtherPower) {
SpawnScript.militia = 0;
SpawnScript.ManAtArms = 0;
}
if (power == OtherPower)
{
OtherPower = OtherPower * System.Math.PI;
SpawnScript.militia = 0;
SpawnScript.ManAtArms = 0;
}
OtherPower = System.Math.Ceiling (OtherPower);
while (damage >= 1){
if (damage >= 10 && SpawnScript.ManAtArms > 1){
SpawnScript.ManAtArms = SpawnScript.ManAtArms - 1;
damage = damage - 10;
}
if (damage >= 1 && SpawnScript.militia > 1){
SpawnScript.militia = SpawnScript.militia - 1;
damage = damage - 1;
}
if (damage < 1 || power < 1){
damage = 0;
power = SpawnScript.militia + SpawnScript.ManAtArms * 10;
}
}
}
Well whenever Unity freezes like that, you usually got yourself an infinite loop. And seeing that you have a while loop there this would be the prime suspect. (maybe another while loop not displayed here could cause it aswell, I don't know the value of power or damage when you run this script, so I can't tell wheter this would freeze it or not)
If it is not infinite, it could be that you are trying to Instantiate a lot of things in the same frame. Though it should in that case start to respond again at some point when it's finished instantiating.
The reason why your Debug.Log is not displaying anything is by the way for the same reason. Because you are most likely stuck in a loop, Unity draws all logs to the display when rendering. Rendering happens at the end of the frame. So if you get stuck somewhere in the frame, it will never log anything.
If you want to debug it, I would advise to change the loop to something like this.
int maxIterations = 9999; // put a number here that you don't expect to reach
while (damage >= 1)
{
if (damage >= 10 && SpawnScript.$$anonymous$$anAtArms > 1)
{
SpawnScript.$$anonymous$$anAtArms = SpawnScript.$$anonymous$$anAtArms - 1;
damage = damage - 10;
}
if (damage >= 1 && SpawnScript.militia > 1)
{
SpawnScript.militia = SpawnScript.militia - 1;
damage = damage - 1;
}
if (damage < 1 || power < 1)
{
damage = 0;
power = SpawnScript.militia + SpawnScript.$$anonymous$$anAtArms * 10;
}
maxIterations--;
if (maxIterations <= 0)
{
Debug.Log("OHOH: " + damage + " : " + power);
break;
}
}
Also, I'm not sure, sinse I don't know what this code is supposed to do, but it could be that you want the second if to be an else if. Because setting both damage to a value greater then 1 and $$anonymous$$anAtArms and militia to 0 will cause an infinite loop. And this is possible if you increase OtherPower enough in the first if get get into the next if.
Thanks, that helped to at least stop the infinite loop. Still haven't worked out the cause, but I appreciate the help.
This enabled me to figure out what was going wrong. I needed >= 1 not just >1. That was the cause.
@mechsrule1 please put that as the answer to your question.
Answer by mechsrule1 · May 20, 2015 at 06:22 AM
The issue was
if (damage >= 10 && SpawnScript.ManAtArms > 1)
{
}
if (damage >= 1 && SpawnScript.militia > 1)
{
}
should have been
if (damage >= 10 && SpawnScript.ManAtArms >= 1)
{
}
if (damage >= 1 && SpawnScript.militia >= 1)
{
}
Your answer

Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Most efficient way to remove a null object from a dictionary 3 Answers
Any way to use multiple font styles within a single UI Text component? 1 Answer
Is it possible to access OnValidate() when using a custom inspector? 1 Answer