(Unity C#)某些方法可以访问堆栈,但有些方法不能访问,将其留空

时间:2018-11-16 15:41:07

标签: c# unity3d methods stack

我的Unity项目中有一个错误,对我来说没有任何意义,为什么简单地说,我有一个带有堆栈的脚本,该脚本代表一个库存位置,当您向其中添加项目时,将其推入正确的广告位,如以下GIF所示:https://imgur.com/a/wxDpvsG

左边是库存,右边是带有随机物品的盒子。我的问题开始于尝试制作鼠标悬停工具提示以从清单中删除项目。这是插槽的脚本。

    public class InvSlots : MonoBehaviour , IPointerEnterHandler, IPointerExitHandler
    {

        public Stack<Item> items = new Stack<Item>();

        public Text stackTxt;

        public Image imgItem;

        public GameObject ToolTip, Exclude;


        public bool IsEmpty
        {
            get { if (items == null) { return true; } return items.Count == 0; }
        }

        public int CountItem()
        {
            return this.items.Count;
        }


        public Item CurrentItem
        {

            get { return items.Peek(); }

        }

        public bool isAvailable
        {

            get { return CurrentItem.maxnumber > items.Count; }

        }

        public void addItem(Item item)
        {
            items.Push(item);
            changeSprite(Resources.Load<Sprite>("imgs/ItemIcons/" + item.iconPath));
            if (items.Count >= 1)
            {
                stackTxt.text = items.Count.ToString();
            }
        }

        private void changeSprite(Sprite sprite)
        {
            imgItem.sprite = sprite;
            imgItem.color = new Vector4(1, 1, 1, 1);
        }


     public void OnPointerEnter(PointerEventData eventData)
    {
        Debug.Log("pointer enter " + items.Peek().nome);
        if (items.Count > 0)
        {
            Instantiate(ToolTip, gameObject.transform.position, new Quaternion(), GameObject.Find("BGInv").transform);
            GameObject.Find("ToolTipNome").GetComponent<TMPro.TextMeshProUGUI>().text = CurrentItem.nome;
            GameObject.Find("ToolTipDesc").GetComponent<TMPro.TextMeshProUGUI>().text = CurrentItem.desc;
        }
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        Debug.Log("pointer exit" + items.Peek().nome);
        if (items.Count > 0)
        {
            Destroy(GameObject.Find("HoverOver Tooltip 1(Clone)"));
        }
    }

    public void OpenExclude()
    {
        Debug.Log("Open Exclude " + items.Peek().nome);
        if (items.Count > 0)
        {
            Destroy(GameObject.Find("HoverOver Tooltip 1(Clone)"));
            Instantiate(Exclude, gameObject.transform.position, new Quaternion(), GameObject.Find("BGInv").transform);
        }

    }

    public void CloseExclude()
    {
        Debug.Log("Close exclude " + items.Peek().nome);
        if (items.Count > 0)
        {
            Destroy(GameObject.FindGameObjectWithTag("ExcludeItems"));
        }
    }

    public void ExcludeOneItem()
    {
        Debug.Log("Exclude 1 " + items.Peek().nome);
        if (items.Count > 0)
        {
            items.Pop();
            stackTxt.text = items.Count.ToString();
            Destroy(GameObject.FindGameObjectWithTag("ExcludeItems"));
        }
    }

    public void ExcludeAllItem()
    {
        Debug.Log("Exclude all " + items.Peek().nome);
        if (items.Count > 0)
        {
            imgItem.color = new Vector4(1, 1, 1, 1);
            items.Clear();
            stackTxt.text = "";
            Destroy(GameObject.FindGameObjectWithTag("ExcludeItems"));
        }
    }
}

所以基本上,我有那个项目堆栈,但是每当我尝试通过CloseExclude(),ExcludeOneItem()和ExcludeAllItem()方法访问堆栈时,堆栈都是空的,但是在脚本的其他所有方法中,堆栈都是空的有喜欢的东西。我不知道为什么会发生这样的事情,我从未见过这样的事情。只是为了清楚起见,这些方法是从按钮的OnClick调用的,但是OpenExclude()也是如此,所以我认为不是这样。 如您在脚本中所看到的,我有一堆日志,这是控制台的图片: Console

还有一个GIF,可以帮助您更清楚地看到它: https://imgur.com/a/KxEsOUl

TLDR我的堆栈无法在脚本的某些方法上使用,但在其他方法上可以正常使用。

1 个答案:

答案 0 :(得分:0)

感谢elgozo,我设法弄对了,排除菜单的预制件确实得到了错误的InvSlots实例!我所做的只是简单地编写一个脚本,以在实例化预制件时设置预制件正在使用的InvSlot,因此不会弄乱它。