Hello,
I’m trying to add object names to a list if the object is clicked and remove it from the list if the object is clicked again.
But it seems like it’s always a new list if I click on an object. If I click an object and print the list, it only contains the clicked object even if I have clicked other objects before and if I click an object again, it says the list was already empty. I’m not sure how I can avoid this and permanently store the object names in one list.
That’s what I have right now:
private bool b = false;
List<string> itemList;
public void Start()
{
itemList = new List<string>();
}
public void OnMouseClick()
{
if (b == false)
{
itemList.Add(this.name);
b = true;
} else
{
if (!itemList.Any())
{
Debug.Log("Empty List");
} else
{
itemList.Remove(this.name);
}
b = false;
}
//print elements on console
if (!itemList.Any())
{
Debug.Log("Empty List");
} else
{
Debug.Log("Not empty List: ");
foreach (string s in itemList)
{
print(s);
}
}