Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Unity freeze by using Invoke

Discussion in 'Scripting' started by Deleted User, Oct 24, 2018.

  1. Deleted User

    Deleted User

    Guest

    hi when i use invoke my unity editor will get frozen,
    can someone help me with one alternative solution ?
    thanks.

    Code (CSharp):
    1.     void RandomTimer()
    2.     {
    3.         float rnd = Random.Range (minEventTime, maxEventTime);
    4.         Invoke ("RandomItem", rnd);
    5.     }
    6.  
    7.     void RandomItem()
    8.     {
    9.         bool isValid = false;
    10.         InteractableObj ObjToChangeState;
    11.         int count = 0;
    12.  
    13.         do
    14.         {
    15.             ObjToChangeState = AllObjs[Random.Range(0, AllObjs.Length)];
    16.             if(!ObjToChangeState.isUsed)
    17.             {
    18.                 ObjToChangeState.isUsed = true;
    19.                 ObjToChangeState.ChangeState(true);
    20.                 isValid = true;
    21.             }
    22.             count++;
    23.         }
    24.         while(!isValid);
    25.  
    26.         RandomTimer ();
    27.     }
     
  2. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    It make freeze if you never meet the condition.

    add some debugs and a sanity check to break out if you get stuck.

    (not actually useful code, but you get the drift.
    Code (CSharp):
    1. int sanity = 0;
    2.  
    3. while(ourCondition == false && sanity < 1000){
    4.  
    5. //DO STUFF
    6.  
    7. sanity++
    8.  
    9. }
    10.  
    11. if(outCondition == false)
    12. Debug.LogError("sanity check bail out");
     
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,146
    Yep, has nothing to do with Invoke, you're just getting stuck in your while loop if isValid never gets set to true, you never exit the loop.
     
  4. Deleted User

    Deleted User

    Guest

    how to fix that?
     
  5. Subliminalman

    Subliminalman

    Joined:
    Sep 11, 2012
    Posts:
    47
    At your while loop check make it do this. You want to make sure that you have a way to exit the loop and since you have a counter already you can use that.
    Code (csharp):
    1.  
    2. do
    3. {
    4.      ObjToChangeState = AllObjs[Random.Range(0, AllObjs.Length)];
    5.      if(!ObjToChangeState.isUsed)
    6.      {
    7.            ObjToChangeState.isUsed = true;
    8.            ObjToChangeState.ChangeState(true);
    9.            isValid = true;
    10.      }
    11.      count++;
    12. }
    13. while(!isValid && count < AllObjs.Length);
    14.  
     
  6. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    So if all objects are used, your code will never exit the loop. Most of the time when unity freezes its because its entered an infinite loop.

    If most objects are used, your code will take multiple cycles to exit the loop. Which is a waste of processing time. If your collection is large, this could easily cost you a frame or two.

    I would strongly suggest using another collection which only contains the unused objects. Then randomly pick from there. If the collection is empty, then you can return an appropriate error.