2015-06-19 46 views
0

我試圖創建一個公共變量,然後引用從腳本附加到的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被證明比預期更復雜。 –

回答

2

以下是您需要的,請參閱文檔here

EventSystem.current 

enter image description here