Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Romano185 · Jul 08, 2012 at 01:15 PM · listfreezeforloop

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];
 }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2

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++ ){
    ...
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Romano185 · Jul 08, 2012 at 02:09 PM 0
Share

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?

avatar image
0

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.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Romano185 · Jul 08, 2012 at 04:05 PM 0
Share

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

avatar image captaincrunch80 · Jul 08, 2012 at 04:36 PM 0
Share

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

avatar image Eric5h5 · Jul 08, 2012 at 04:49 PM 0
Share

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

avatar image captaincrunch80 · Jul 08, 2012 at 04:51 PM 0
Share

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

 TestCodeComment();
avatar image Romano185 · Jul 08, 2012 at 06:33 PM 0
Share

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?

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

GameObject List and For Loop with OnGUI 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges