Shai Segal
Shai Segal

Reputation: 1

Text on buttons issue - Unity 3D game

I'm developing a 3D game for Android platform in first person style. The idea is when the player clicks an object (Item), the object gets disappeared. Then this object item name appears on a button which belongs to items bar that the player can see. The problem begins when the player chooses any item by clicking on it's button and choose another object in the game to interact with. The object item name needs to be removed from the button, but it doesn't. I have tried several ways to make the item names to get removed, but they're still shown.

The code:

    [code=CSharp]public static List<string> requestedItems = new List<string>();
public static List<GameObject> items = new List<GameObject>();
public static List<bool> itemsBoolean = new List<bool>();
public List<string> buttonsname = new List<string>();
public List<Text> buttonsText = new List<Text>();

void Update()
    {
        // items is the list of Gameobjects which the player clicks on and needs to be added
           to a button with it's name
        if (items != null)
        {
           // Originizing the buttons text depending the item names (If there are)
           for (int i = 0; i < items.Count; i++)
           {
               //Debug.Log(items[i]);
              
               buttonsText[i].text = items[i].name;
           }
        }

// The on-click function related to the buttons in the panel - marks "true" on item when player pushes his button
    public void ItemPanelClick(GameObject gameObject)
    {
        if (items != null)
        {
            string buttonObjectName = gameObject.name;
            for (int j = 0; j < items.Count; j++)
            {
                if (buttonsname[j] == buttonObjectName)
                {
                    itemsBoolean[j] = true;
                }
            }
        }
    }

  // When item is not necessary, the function removes the item from the 2 arrays
     items array - the selected object items list
     items boolean - bool list for items if their buttons were selected already or not

    public void removeItemFromArrays(int index)
    {
        List<GameObject> itemsTemp = new List<GameObject>();
        List<bool> itemsBooleanTemp = new List<bool>();
        for (int i = 0; i < items.Count; i++)
        {
            if (i == index)
            {
                continue;
            }
            itemsTemp.Add(items[i]);
            itemsBooleanTemp.Add(itemsBoolean[i]);
        }
        items = new List<GameObject>();
        itemsBoolean = new List<bool>();
        for (int j = 0; j < itemsTemp.Count; j++)
        {
            items.Add(itemsTemp[j]);
            itemsBoolean.Add(itemsBooleanTemp[j]);
        }

}

Every help/idea with the code is appreciated. Thanks in advance!

Upvotes: 0

Views: 70

Answers (1)

Abdessamad EL
Abdessamad EL

Reputation: 436

You are not removing the item from the arrays. Use array.Remove(item) Or array.RemoveAt(index)

Here is your method:

public void removeItemFromArrays(int index)
            {
                items.RemoveAt(index);
                itemsBoolean.RemoveAt(index);
            }

Upvotes: 0

Related Questions