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 crash after Invoke function

Discussion in 'Scripting' started by kondor, Sep 5, 2017.

  1. kondor

    kondor

    Joined:
    Jun 18, 2013
    Posts:
    48
    HI, I wrote this code:

    Code (CSharp):
    1. void generatePlatform()
    2.     {
    3.         if (platforms.Count >= 10 && pulled())
    4.         {
    5.             print("a");
    6.             selectedPlatform.transform.localScale = new Vector3(selectedPlatform.transform.localScale.x, selectedPlatform.transform.localScale.y, w);
    7.             selectedPlatform.transform.position = nextPos;
    8.         }
    9.         else
    10.         {
    11.             selectedPlatform = Instantiate(platform, nextPos, Quaternion.identity);
    12.             selectedPlatform.transform.localScale = new Vector3(selectedPlatform.transform.localScale.x, selectedPlatform.transform.localScale.y, w);
    13.             selectedPlatform.name = "Platform" + platforms.Count;
    14.             addPlatform();
    15.         }
    16.         recallFunction();
    17.     }
    18.  
    19.     public void addPlatform()
    20.     {
    21.         if(!platforms.Contains(selectedPlatform))
    22.         {
    23.             platforms.Add(selectedPlatform);
    24.         }
    25.     }
    26.  
    27.     bool pullPlatform()
    28.     {
    29.         int i = 0;
    30.         while(i <= platforms.Count)
    31.         {
    32.             if (platforms.transform.position.z < player.transform.position.z)
    33.             {
    34.                 selectedPlatform = platforms;
    35.                 i++;
    36.                 return true;
    37.             }
    38.         }
    39.         return false;
    40.     }
    41.  
    42.     void recallFunction()
    43.     {
    44.         Invoke("randomValues", 1f);
    45.     }
    46.  
    47.     bool pulled()
    48.     {
    49.         int i = 0;
    50.         while(i <= platforms.Count)
    51.         {
    52.             if (platforms.transform.position.z < player.transform.position.z)
    53.             {
    54.                 selectedPlatform = platforms;
    55.                 i++;
    56.                 return true;
    57.             }
    58.         }
    59.         return false;
    60.     }
    61. }

    And unity all the time crashes after it's
    Invoke function when it's return true in the if statement:
    if (platforms.Count >= 10 && pulled())
    please help it never happened before
    Thanks for the help.
     
    Last edited: Sep 5, 2017
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,146
  3. kondor

    kondor

    Joined:
    Jun 18, 2013
    Posts:
    48
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,146
    Ok, but still fix your post with code tags. Hard to read as is.

    If it freezes up, you have an infinite loop probably. I would make sure you're not getting stuck in your while loops. If it never exits a while loop, it will freeze up.