如何从脚本引用UI事件系统?

问题描述:

我试图创建一个公共变量,然后引用从脚本附加到的gameObject的检查器的EventSystem对象,但每当我尝试从EventSystems访问变量时,我得到一个NullReference异常:对象引用不是设置为一个对象的实例。如何从脚本引用UI事件系统?

我试着用GameObject.Find()找到它,但显然我不能将gameObject转换成EventSystem.EventSystem与内置演员。

我不知道该怎么做。我只是想访问

EventSystem.currentSelectedGameObject.name

找出哪些UI对象在画布被选中。

+0

我原本以为它利用'EventSystem.current.currentSelectedGameObject.name'但我仍然得到同样的NullReference例外... –

+0

显然'EventSystem.current.currentSelectedGameObject'返回null一切。我想这是问题。我不明白为什么它认为我没有选择任何东西,因为我明显可以使画布中的Scroll Rect对象正常工作。 –

+0

以'Camera.main.ScreenPointToRay(Input.mousePosition)'和'Physics.Raycast()'为代价去寻找gameObject,因为EventSystem被证明比预期更复杂。 –

以下是您需要的,请参阅文档here

EventSystem.current 

enter image description here