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