¿How Can I fill an array in runtime?

Hi folks having an issue that’s driving me crazy.
I’m working on a game mechanic in which you type a sequence of buttons and if that sequence matches with an existing one then you spawn a specific game object, kinda like a combo but not exaclty.

Here’s the code with further explanation:

using System.Collections;
using System.Collections.Generic;
using System.Linq; // allows to compare contents of two arraws with a built-in function (SequenceEqual)


using UnityEngine;

public class BuilderMode : MonoBehaviour
{
    public PlayerMovement playerMovement;
    public bool onBuilderMode;
    public bool arraysAreEquals;


    public int[] testComboArray = { 1, 2, 3 };
    public int[] testComboInputArray = new int[3];

    //combo Inputs:
    public KeyCode metal = KeyCode.Q;
    public KeyCode spark = KeyCode.W;
    public KeyCode ammo = KeyCode.E;

    private int q_metalCode = 1;
    private int w_sparkCode = 2;
    private int e_ammoCode = 3;
    public int comboSlotValue;


    void Start()
    {
        onBuilderMode = false;
        arraysAreEquals = false;

        for (int i = 0; i < testComboInputArray.Length; i++)
        {
            testComboInputArray *= 0;*

}

//for each (int i in testComboInputArray) print (i);
}

void Update()
{
StartBuilderMode();
CancelBuilderMode();
FillInputArray();
printComboKeysValues();

if (onBuilderMode == true && Input.GetButtonDown(“Fire1”))
{
CheckArrays();
}

}

void StartBuilderMode()
{
if (Input.GetButtonDown(“BuilderMode”))
{
onBuilderMode = true;
playerMovement.movementSpeed_magnitude = 0;

}
}

void CancelBuilderMode()
{
if (onBuilderMode == true && Input.GetButtonDown(“Fire2”))
{
onBuilderMode = false;
playerMovement.movementSpeed_magnitude = playerMovement.walkSpeed;

comboSlotValue = 0;
for (int i = 0; i < testComboInputArray.Length; i++)
{
testComboInputArray = 0;
}

}
}

void printComboKeysValues()
{
if (onBuilderMode == true)
{
if (Input.GetKeyDown(metal))
{
comboSlotValue = q_metalCode;
}
if (Input.GetKeyDown(spark))
{
comboSlotValue = w_sparkCode;
}
if (Input.GetKeyDown(ammo))
{
comboSlotValue = e_ammoCode;
}
}
}

void CheckArrays()
{
if (testComboInputArray.SequenceEqual(testComboArray))
{
arraysAreEquals = true;

}
else { arraysAreEquals = false; }
}

void FillInputArray()
{
if (onBuilderMode == true)
{
int i = 0;
int range = testComboInputArray.Length;
////

if (i < range)
{

if (testComboInputArray > 0)
{
i++;
}

if (i < range)
{
testComboInputArray = comboSlotValue;
}

}

////
print("ArrayInout Length is " + range);
print("filling Array Slot " + i);

}
}
They way I’m trying to solve it is by creating two arrays:
public int[] testComboArray = { 1, 2, 3 }; this is the array that has already a sequence.
public int[] testComboInputArray = new int[3]; and this is the empty array that the player is supposed to fill.
----------
What I did was asign int values to the QWE keys in the printComboKeysValues() basically if you press the Q key the variable comboSlotValue changes to 1, If you hit W it becomes 2 and E is 3.
The real problem starts in the FillInputArray() method basically what I’m trying to do is the player assings the first array slot (testComboInputArray[0]) to the comboSlotValue variable (which can be 1,2 or 3) after the slot is filled then I check if the value of the slot is greater than zero, meaning is no “empty” anymore then increase i by 1 to move into the [1] slot and finally [2] but it never makes it past [1]. the i++ stops adding and never reaches [2]
the two solutions I tried were:
void FillInputArray()
{
if (onBuilderMode == true)
{
int i = 0;
int range = testComboInputArray.Length;
////

while (i < range)
{
testComboInputArray = comboSlotValue;

if(testComboInputArray > 0)
{
i++;
}

}

////
print("ArrayInout Length is " + range);
print("filling Array Slot " + i);

}
}
this one “crashes” Unity because it creates an infinite loop.
and:
void FillInputArray()
{
if (onBuilderMode == true)
{
int i = 0;
int range = testComboInputArray.Length;
////

if (i < range) //check if i is smaller than the array range
{
int currentSlot = testComboInputArray*;//get slot to modify*

currentSlot = comboSlotValue; //the current slot will be added the value of the player input

if (currentSlot > 0) // if the current slot is not zero then move to next slot
{
i++;
}
}

////
print("ArrayInout Length is " + range);
print("filling Array Slot " + i);

}
}

this one never adds up i, any ideas of to what Im doing wrong or not doing?

For the sake of helping, here is a small code that takes an input to be checked against an expected input:

public class Test : MonoBehaviour {

    List<string> list = new List<string>();
    List<string> readAgainst = new List<string>() { "a", "b", "c", "d" };
    private float timer = 0;
	void Update ()
    {
        timer += Time.deltaTime;
        if (Input.anyKeyDown)
        {
            timer = 0f;
            string str = Input.inputString;
            list.Add(str);
            CheckContent();
        }
        if(timer > 1f)
        {
            list.Clear();
        }
	}
    void CheckContent()
    {
        if(list.Count < readAgainst.Count) { return; }
        if(list.Count > readAgainst.Count) { list.RemoveAt(0); }
        for(int i = 0; i < list.Count; i++)
        {
            if(list_.Equals(readAgainst*) == false) { return; }*_

}

Debug.Log(“Build”);
}
}
it has first a list in which you will store the input. The second list is the expected input (abcd).
There is also a timer so the each new key has 1s to be entered or it clears the list. This avoids unexpected action such as typing xzab and then later cdef and seeing a build you would not expect.
It checks if any key is down, then resets the timer to 0 and adds that key to the list.
Moves on to check if the current state of the list is matching the readAgainst list (our expected input).
If the two lists are not same length, stop here. if the list is larger that our readAgainst then remove the first one. This is not perfect, you should still check more for length but considering you would not enter two keys at once, it will be fine for now.
If the two list are same count, then we move on to compare, if any key different, we return from method.
If the loop goes all the way, it means we have the same two collections.

If I understand what you are trying to do with that function, this should do the trick. Each call puts the value of comboSlotValue into the next available spot of testComboInputArray

void FillInputArray()
{
    if (!onBuildMode) return;
	
	for (int i = 0; i < testComboInputArray.Length; i++)
	{
	    // If this slot hasn't been set...
		if (testComboInputArray *!= 0)*
  •  {*
    
  •      // Set it*
    

_ testComboInputArray = comboSlotValue;_
* // We are done with this input. Next call will fill the next slot.*
* return;*
* }*
* }*

* Debug.logError(“The combo input array was already filled”);*
}

@Rootlan @PizzaPie

To get rid of the array is full error, shouldn’t the loop check that the slot is NOT full or == 0?
i.e. if (testComboInputArray == 0
I could be wrong but sounds right to me. Otherwise you are saying if the array slot is not zero, then do something. Well, it is 0 at first. So, you should say, if array slot is empty (0), then do this.