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

Question Unity crashing when I try to start game

Discussion in 'Scripting' started by woodkir000, Apr 12, 2022.

  1. woodkir000

    woodkir000

    Joined:
    Jan 13, 2022
    Posts:
    18
    Hey all, I'm encountering a problem I'm not sure how to fix. To preface this, I'm making a turn-based strategy game and am trying to make it so I iterate through each character to do a set amount of moves in a team. Here's the code:

    Code (CSharp):
    1. private async void Update() {
    2.             foreach (GameObject i in t1sprites) {
    3.                 var moveCount = 3;
    4.                 while (moveCount > 0) {
    5.                     if (Input.GetKeyDown(KeyCode.W)) {
    6.                         i.transform.Translate(new Vector3(0, 0, 1));
    7.                         moveCount--;
    8.                     }
    9.                     if (Input.GetKeyDown(KeyCode.S)) {
    10.                         i.transform.Translate(new Vector3(0, 0, -1));
    11.                         moveCount--;
    12.                     }
    13.                     if (Input.GetKeyDown(KeyCode.A)) {
    14.                         i.transform.Translate(new Vector3(-1, 0, 0));
    15.                         moveCount--;
    16.                     }
    17.                     if (Input.GetKeyDown(KeyCode.D)) {
    18.                         i.transform.Translate(new Vector3(1, 0, 0));
    19.                         moveCount--;
    20.                     }
    21.                 }
    22.             }
    23. }
    As for the crash, whenever I try to start it, Unity stops working and if I tab out I can't tab back in. It doesn't recover and force quitting it doesn't work
     
  2. Zalosath

    Zalosath

    Joined:
    Sep 13, 2014
    Posts:
    671
    You have a while loop in your update method. This will run indefinitely since the game will crash before you can start registering key presses.
     
    Joe-Censored likes this.
  3. woodkir000

    woodkir000

    Joined:
    Jan 13, 2022
    Posts:
    18
    Ah, thanks. Is there an easy fix to change this code to make it so that I can loop through each character to do their moves one at a time?
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,748
    You probably should change to using a coroutine. You can put a yield inside a while loop in a coroutine, and it'll wait a frame before continuing execution.

    Code (csharp):
    1. void Start() {
    2. StartCoroutine(MovementCoroutine());
    3. }
    4.  
    5. IEnumerator MovementCoroutine() {
    6.             foreach (GameObject i in t1sprites) {
    7.                 var moveCount = 3;
    8.                 while (moveCount > 0) {
    9.                     if (Input.GetKeyDown(KeyCode.W)) {
    10.                         i.transform.Translate(new Vector3(0, 0, 1));
    11.                         moveCount--;
    12.                     }
    13.                     if (Input.GetKeyDown(KeyCode.S)) {
    14.                         i.transform.Translate(new Vector3(0, 0, -1));
    15.                         moveCount--;
    16.                     }
    17.                     if (Input.GetKeyDown(KeyCode.A)) {
    18.                         i.transform.Translate(new Vector3(-1, 0, 0));
    19.                         moveCount--;
    20.                     }
    21.                     if (Input.GetKeyDown(KeyCode.D)) {
    22.                         i.transform.Translate(new Vector3(1, 0, 0));
    23.                         moveCount--;
    24.                     }
    25.                     yield return null;
    26.                 }
    27.             }
    28. }
     
  5. Zalosath

    Zalosath

    Joined:
    Sep 13, 2014
    Posts:
    671
    Not with your current implementation.

    Have an integer that will act as an index to your t1sprites array/list, allow only the current index to move and then update the index. So on and so fourth.
     
  6. woodkir000

    woodkir000

    Joined:
    Jan 13, 2022
    Posts:
    18
    This is working perfectly, thank you so much!