一种方法中的堆栈对象似乎与 UNITY 中的另一种方法不同?

Stack object in one method does not seem to be the same as in another in UNITY?

我有一段这样的代码。问题是 push 和 pop 不能一起工作。实际上,我认为 push 方法运行良好,但 pop 存在问题。因为当对象触发 stack.count 增加 1 但是当点击 canvas 中的按钮时,控制台打印“The Stack is Empty”并且 stack.count = 0。这里有什么问题?

    Stack<Skill> stackSkills;
   
    void Start()
    {
        stackSkills= new Stack<Skill>();
    }

    void OnTriggerEnter(Collider collider){
     
            stackSkills.Push(newSkill);
            Debug.Log(stackSkills.Count); //When i trigger it , it increases.
    }

    public void OnClick(){

        Debug.Log("clicked");
        Debug.Log(stackSkills.Count);
        stackSkills.Pop();
        
    }
}

示例如下: 输出

1
2
3
clicked
0
InvalidOperationException: Stack empty.
System.Collections.Generic.Stack1[T].ThrowForEmptyStack () (at <ef151b6abb5d474cb2c1cb8906a8b5a4>:0)

发生此问题的原因是在不同的两个游戏对象(Canvas 和 myGameObject)中使用了相同的脚本,并且它们没有与每个 other.I 调用 OnTriggerEnter() 链接myGameObject 中的方法和 Canvas 中的 OnClick() 方法。因此他们不共享相同的数据字段。项目中有两个Stack。