【问题标题】:(Unity C#) Some methods can access a Stack, but some can't, leaving it blank(Unity C#) 有些方法可以访问堆栈,但有些不能,将其留空
【发布时间】:2018-11-16 15:41:07
【问题描述】:

我的 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 我有一个堆栈不适用于我脚本的某些方法,但在其他方法中运行良好。

【问题讨论】:

  • 为什么你的错误计数突然从 3 变成了 9 而没有调用 gif 中的任何函数?
  • 我的鼠标悬停在没有任何项目的插槽上,并且由于我的脚本有一个日志试图显示项目的名称,所以他得到了所有这些错误
  • 你检查过this
  • 我做了,是的,但我不认为这是同一个问题,这个似乎一般的用户界面有问题,我的大部分工作都在工作
  • 那里的小上下文菜单(或带有处理程序的按钮)似乎在不同的、单独的 InvSlots 实例上工作......

标签: c# unity3d methods stack


【解决方案1】:

感谢elgozo 我设法弄对了,排除菜单的预制件确实得到了错误的 InvSlots 实例!我所做的只是简单地编写一个脚本来设置预制件在实例化时使用的 InvSlots,这样就不会搞砸了。

【讨论】:

    猜你喜欢
    • 2016-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多