从列表和场景中删除最后一个实例化的GameObject [英] Deleting the last Instantiated GameObject from a List and Scene

查看:191
本文介绍了从列表和场景中删除最后一个实例化的GameObject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我缺少了一些可能对某些人来说很简单的东西,但是我遇到了麻烦.我创建了一个List(msgSymbols),每次按下按钮(基本上是自定义键盘)后,该列表就会填充一个新的GameObject,然后实例化为Grid Layout.

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.

但是我该如何只删除列表上的最后一个GameObject并更新Grid Layout以反映该更改? (例如点击键盘上的退格键)?我当前拥有的删除键代码将引发错误:ArgumentOutOfRangeException:参数超出范围.参数名称:索引

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

我该如何解决?

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

推荐答案

此处的代码未在对象上调用Destroy().从列表中删除它不会消失,因为Unity会独立于您所做的任何事情跟踪所有gameObjects. Destroy()Instantiate()是从游戏中删除或添加游戏对象的方法.

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.

这篇关于从列表和场景中删除最后一个实例化的GameObject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆