x


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];
}
}
more ▼

asked Jul 08 '12 at 01:15 PM

Romano185 gravatar image

Romano185
35 13 20 22

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Because i is always < 1 and you never exit the for-loop. Correct syntax is:

for (i = 0; i < 1; i++ ){
   ...
}
more ▼

answered Jul 08 '12 at 01:43 PM

delstrega gravatar image

delstrega
840 5 8 12

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?

Jul 08 '12 at 02:09 PM Romano185
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Jul 08 '12 at 03:48 PM

captaincrunch80 gravatar image

captaincrunch80
240 1 3 4

How would you suggest me to find the highest value in an array and keep doing that without using a for-loop?

Jul 08 '12 at 04:05 PM Romano185

I just saw again I need an answer to post code nicely... So please see above.

Jul 08 '12 at 04:36 PM captaincrunch80

You can post code formatted properly in comments the same way as answers, just make sure there are 4 spaces before each line.

Jul 08 '12 at 04:49 PM Eric5h5

Ah thanks for the hint buddy, I just used 3 spaces and it was all messed up.

TestCodeComment();
Jul 08 '12 at 04:51 PM captaincrunch80

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?

Jul 08 '12 at 06:33 PM Romano185
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x356
x177
x13

asked: Jul 08 '12 at 01:15 PM

Seen: 474 times

Last Updated: Jul 08 '12 at 06:37 PM