- Home /
Why does this script freeze Unity
Hi everybody
I wrote this script and tested it. Unity did not display an error, so I thought it was safe. I attached it to a empty GameObject and added a trigger. But when my player character, tagged "Player" enters the trigger, Unity completely freezes and I have to shut it down. I expect a problem with the for loop. I want this script to check all aggro variables, which are being set by another script, and find the GameObject which holds the same position in the first list which has the highest aggro. I want this to be done all the time until another script destroys the GameObject it's attached to.
import System.Collections.Generic;
var players : List.<GameObject> = new List.<GameObject>();
var aggros : List.<int> = new List.<int>();
var highestaggro : int = -1;
var checkednumber : int = 0;
var target : GameObject = null;
function OnTriggerEnter (trigger : Collider){
if(trigger.tag == "Player"){
players.Add(trigger.gameObject);
aggros.Add(0);
if (target == null){
AggroCheck ();
}
}
}
function AggroCheck (){
for (i = 0; i<1; ){
if (checkednumber == aggros.Count){
checkednumber = 0;
}
if (aggros[checkednumber] > highestaggro){
target = players[checkednumber];
highestaggro = aggros[checkednumber];
}
}
Answer by delstrega · Jul 08, 2012 at 01:43 PM
Because i is always < 1 and you never exit the for-loop. Correct syntax is:
for (i = 0; i < 1; i++ ){
...
}
I don't want the loop to run once. I want it to run always until the health of the enemy it's attached to reaches 0.
import System.Collections.Generic; var players : List. = new List.(); var aggros : List. = new List.(); var highestaggro : int = -1; var checkednumber : int = 0; var target : GameObject = null; var enemyhealth : EnemyHealth;
function Start () { enemyhealth = gameObject.GetComponent(EnemyHealth); }
function OnTriggerEnter (trigger : Collider){ if(trigger.tag == "Player"){ players.Add(trigger.gameObject); aggros.Add(0); if (target == null){ AggroCheck (); } } }
function AggroCheck (){ for (i = 0; i<1; ){ if (checkednumber == aggros.Count){ checkednumber = 0; } if (aggros[checkednumber] > highestaggro){ target = players[checkednumber]; highestaggro = aggros[checkednumber]; } if (enemyhealth.enemyhealth <= 0){ i = 1; } } }
Do I have to use something else it I want this script to loop forever?
Answer by captaincrunch80 · Jul 08, 2012 at 03:48 PM
I believe you are missing some basic programming concepts.
It is like delstrega said.
for (i = 0; i<1; ){ // Freeze!
}
will loop forever. An the main thread will never return to the unity backend. - You are never allowed to block the main thread forever. (Well you are allowed, but it will freeze).
If you want to have anything checked as often as possible put it into the Update() function. It will be called every time a screen is rendered. Another approach (if every lets say 100ms is enought) would be some kind of Coroutine or Invoke implementation.
Answer to your comment:
Well of course you can do it with a for loop, but that loop should not run forever.
I do my stuff in C# mostly, so I try to give my best in javascript:
var length : int = myArray.length;
var maxValue;
var maxValueIndex : int = -1;
if(length > 0) {
maxValue = myArray[0];
maxValueIndex = 0;
for(var i : int = 0; i < length; i++) {
if(myArray[i] > maxValue){
maxValue = myArray[i];
maxValueIndex = i;
}
}
}
// maxValue and maxValueIndex are valid if maxValueIndex >= 0
Now you can feel free too optimize it. If this is called very often, you can use member variables to trade some memory for speed.
How would you suggest me to find the highest value in an array and keep doing that without using a for-loop?
I just saw again I need an answer to post code nicely... So please see above.
You can post code formatted properly in comments the same way as answers, just make sure there are 4 spaces before each line.
Ah thanks for the hint buddy, I just used 3 spaces and it was all messed up.
TestCodeComment();
I have an even better idea for my script. I would like my for-loop to run once when a value inside the array aggros is changed. Is there a special way to check if a value inside an array changed?
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Problem with array 2 Answers
Randomize Text on 4.6 UI Button's 1 Answer
Trying to diagnose this For Loop issue, for instantiating from a list. 0 Answers