Deleting the last Instantiated GameObject from a List and Scene

北战南征 提交于 2019-12-12 00:19:23

问题


So I'm missing something that's probably pretty straightforward to some, but I'm having trouble with it. I've created a List (msgSymbols) that gets populated with a new GameObject every time a button gets pressed (it's basically a custom keyboard) which then gets instantiated to a Grid Layout.

But how do I then delete only the last GameObject on that list and update the Grid Layout to reflect that change? (like hitting a backspace button on the keyboard)? The delete key code that I currently have is throwing error: ArgumentOutOfRangeException: Argument is out of range. Parameter name: index

How do I resolve this?

public string prefabPath;

List<GameObject> msgSymbols = new List<GameObject>();
Vector3 symbolPos = new Vector3(0, 0, 0);
GameObject currentChar;
GameObject msgPanel;
Vector3 symbolScale = new Vector3(1.0f, 1.0f, 1.0f);

GameObject[] charKeys;
GameObject deleteKey;

private int index = 0;

void Start()
{
    msgPanel = GameObject.FindGameObjectWithTag("MessagePanel");
    charKeys = GameObject.FindGameObjectsWithTag("SymbolKey");
    deleteKey = GameObject.FindGameObjectWithTag("DeleteKey");
}

#region IPointerClickHandler implementation

public void OnPointerClick (PointerEventData eventData)
{
    if (transform.CompareTag("SymbolKey"))
    {
        // Load a GameObject into the msgSymbols List
        // and store the last character added in a variable (lastChar)

        msgSymbols.Add((GameObject)Resources.Load(prefabPath));         
        currentChar = msgSymbols.Last<GameObject>();    

        // Instantiate the last character (lastChar) added to msgSymbols List

        GameObject symbol = Instantiate(currentChar, symbolPos, Quaternion.identity) as GameObject;

        // Define transforms for symbol

        symbol.transform.SetParent(msgPanel.transform);
        symbol.transform.localScale = symbolScale;
    }

    if (transform.CompareTag("DeleteKey"))
    {
        Debug.Log (currentChar);

        GameObject toDestroy = msgSymbols[msgSymbols.Count - 1];
        msgSymbols.RemoveAt(msgSymbols.Count - 1);
        Destroy(toDestroy);

        Debug.Log(msgSymbols.Count);
    }
}
#endregion

回答1:


It seems like your problem is that you have a GameObject that isn't going away when you want to get rid of it. I don't know how the code you aren't showing here works, but it seems possible that you are removing the object from the list, which doesn't have any effect on your scene because the GameObject still exists as part of the scene. Just removing it from the list won't have any effect on it by default. Try calling Destroy on the object after removing it from the list.

Debug.Log (lastChar);

GameObject toDestroy = msgSymbols[msgSymbols.Count - 1];
msgSymbols.RemoveAt(msgSymbols.Count - 1);
Destroy(toDestroy);

Debug.Log(msgSymbols.Count);



回答2:


The code here isn't calling Destroy() on the object. Removing it from the list doesn't make it go away, since Unity keeps track of all gameObjects independent of anything you do. Destroy() and Instantiate() are the ways to remove or add gameObjects from the game.



来源:https://stackoverflow.com/questions/31412526/deleting-the-last-instantiated-gameobject-from-a-list-and-scene

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!