Jamie Blacknell
Jamie Blacknell

Reputation: 23

Unbreakable Loop in Unity, not sure how to fix. Causing Unity to crash

I'm trying to create a "combo" system in unity but I've created an Unbreakable loop which I'm struggling to understand. I've tried using a while loop and had this issue so wanted to give a for loop a go as well, but I'm having the same result.

The way the combo system should work is that when the player enters a condition with an enemy, they can enter a chain of buttons on the controller to trigger a combo. If the player enters the correct combo, effects are applied. So far I'm only worried about getting the combo system work.

I've created a pre-defined array of chars for the combo, and then I compare an input from the player to this array.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ComboSystem : MonoBehaviour
{
    public char currentButton;
    public char[] combo_arr = { 'A', 'A', 'B', 'B', 'X' };

    PlayerLockOnSystem plos;

    private void Start()
    {
        plos = GetComponent<PlayerLockOnSystem>();
    }

    private void Update()
    {
        if (plos.lockedOn)
        {
            Combo();
        }
    }

    void DetectComboButtons()
    {
        if (Input.GetButton("Joystick A"))
        {
            currentButton = 'A';
        }

        if (Input.GetButton("Joystick X"))
        {
            currentButton = 'X';
        }

        if (Input.GetButton("Joystick B"))
        {
            currentButton = 'B';
        }


    }

    void Combo()
    {



        for (int i = 0; i < combo_arr.Length; i++)
        {
            DetectComboButtons();

            if (currentButton == combo_arr[i])
            {

                Debug.Log("Correct: " + currentButton);
            }
            else
            {
                i = 0;
                Debug.Log("Incorrect");
            }
        }






    }




}

When the Combo() method is triggered, Unity crashes and I have to force close the editor.

Upvotes: 0

Views: 196

Answers (1)

Tomer Shahar
Tomer Shahar

Reputation: 343

You need to check the input between frames, in different calls of update. Try this:

private const char CHAR_THAT_MEANS_THAT_THE_PLAYER_DIDNT_BREAK_THE_CHAIN = '0';//doesnt matter

private int _currentPlaceInTheComboChain = 0;

private void Update()
{
    if (plos.lockedOn)
    {
        Combo();
    }
}

char DetectComboButtons()
{
    if (Input.GetButtonDown("Joystick A"))
    {
        return 'A';
    }

    if (Input.GetButtonDown("Joystick X"))
    {
        return 'X';
    }

    if (Input.GetButtonDown("Joystick B"))
    {
        return 'B';
    }
 return CHAR_THAT_MEANS_THAT_THE_PLAYER_DIDNT_BREAK_THE_CHAIN;


}

void Combo()
{
    char currentButton = DetectComboButtons();
    if (currentButton == CHAR_THAT_MEANS_THAT_THE_PLAYER_DIDNT_BREAK_THE_CHAIN) 
    {
       return; //the player didn't continue the combo but didn't break it (yet)
    } 

    if (currentButton == combo_arr[_currentPlaceInTheComboChain])
    {
       _currentPlaceInTheComboChain++;//wait for the next button, will only be checked the next time update is called
       Debug.Log("Correct: " + currentButton);
       if (_currentPlaceInTheComboChain == combo_arr.Length) { //this was the last button in the combo
           _currentPlaceInTheComboChain = 0; //for a new combo
           Debug.Log("Combo completed");
       }
    } else {
      _currentPlaceInTheComboChain = 0; //player broke the chain
      Debug.Log("Incorrect " + currentButton);
    }

}

Upvotes: 1

Related Questions