SOLVED: Holding list elements in place when removing element 0

Hi guys,

I’ve been struggling with character selection and deselection in a simple word game I’ve been attempting to learn with (basic skills at the moment!) and I’ve come to a point where it seems I need to somehow lock elements in place in an array when I remove an element closer to 0.

For example:
element 1 = N
element 2 = E
element 3 = W

When removing N, E & W will shift backwards up the list meaning that I can no longer add new letters to the beginning of the word.

Here is the function along with the var declarations, I hope it’s clear, any help is much appreciated, I went with lists as I struggled with instantiating to a specific position when using an extra gameobject array.

//Applicable Vars
//entry field tile co-ords
    public Vector2[] userSelectedTilePositions;
//storage of selected randomised tiles
    public GameObject[] selectedTiles;
//storage of tiles selected by user
    public GameObject[] userSelectedTiles;
//List of user selected tiles sorted by selection order
    public List<GameObject> userSelectedList;

//Functions
#region Touch/Click input function - Raycast
    private RaycastHit2D Hit2D()
    {
        Vector2 worldPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        RaycastHit2D hit = Physics2D.Raycast(worldPoint, Vector2.zero);

        return hit;
    }
    #endregion

   
    #region Select and deselect functions
    private void Select()
    {
       
        for (int s = 0; s < userSelectedTiles.Length; s++)
        {
           
            //Select function
            if (Hit2D().collider == selectedTiles[s].GetComponent<Collider2D>() && selectedTiles[s].GetComponent<LetterProperties>()._isSelected == false)
            {
              
                selectedTiles[s].GetComponent<LetterProperties>()._isSelected = true;
                               
                userSelectedList.Add(Hit2D().collider.gameObject);
                userSelectedTiles[s] = Hit2D().collider.gameObject;

                int v = userSelectedList.FindIndex(o => o == Hit2D().collider.gameObject);

                userSelectedTiles[s] = Instantiate(Hit2D().collider.gameObject, userSelectedTilePositions[v], Quaternion.identity);
                Debug.Log(v);
            }
            //Deselect function
            else if (userSelectedTiles[s] != null && Hit2D().collider == userSelectedTiles[s].GetComponent<Collider2D>() || Hit2D().collider == selectedTiles[s].GetComponent<Collider2D>() && selectedTiles[s].GetComponent<LetterProperties>()._isSelected == true)
            {
               
                selectedTiles[s].GetComponent<LetterProperties>()._isSelected = false;
               
                Destroy(userSelectedTiles[s].gameObject);
                userSelectedTiles[s] = null;
                Debug.Log(s);
                userSelectedList.RemoveAt(userSelectedList.FindIndex(o => o == Hit2D().collider.gameObject));
               

            }          

        }

    }
    #endregion

Don’t use the Remove() method above: that’s going to remove it and leave no hole.

Instead, when you want to remove the first element, just set its contents to null:

userSelectedList[ whichElement] = null; // this means it's an open slot

Then consider anything with a null in it to be empty.

I’m also confused why you can’t add new letters to the start of the word? I’m not sure what you are doing, but list allow you to insert into them, even at the start of it if you wanted.

Thanks a lot Kurt, heading this way causes another problem though as these null slots are never treated as open, perhaps a tweak is needed to my “int v = userSelectedList.FindIndex(o => o == Hit2D().collider.gameObject);” line.

Which I think ties into the comment from @Brathnann

@Kurt-Dekker & @Brathnann thank you both for pointing me in the right direction, means a lot :slight_smile:

I got around the issue by adjusting “v” and adding some new modifiers to it.

int v = userSelectedList.FindIndex(0, o => o == null);
userSelectedList.RemoveAt(v);
userSelectedList.Insert(v, Hit2D().collider.gameObject);