2012-08-29 59 views
-1

我得到這個代碼的NullReferenceException

if (Input.GetMouseButton(0)){ 
var ray = Camera.main.ScreenPointToRay(Input.mousePosition); 
     ...... 
    }` 

它與我創建的其他項目,但是當我試圖實現它與我的新項目,它返回一個NullReferenceException。

NullReferenceException 
UnityEngine.Camera.ScreenPointToRay (Vector3 position) (at C:/BuildAgent/work/300357e52574df36/Runtime/ExportGenerated/Editor/UnityEngineCamera.cs:276) 
Ball.Update() (at Assets/Scripts/Ball.js:51)` 

錯誤指向這一部分VAR光= Camera.main ..... 我試圖與場景中添加光但它仍然不工作。 請告訴我什麼是解決方案。通過整個代碼打算移動或拖動對象/球的方式。

+0

可能重複[什麼是NullReferenceException,我該如何解決它?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-doi-i-fix-它) – LearnCocos2D

回答

1

Camera.main返回第一個啓用場景中帶有「MainCamera」標籤的攝像機。如果Camera.mainnull,那麼您可能已經刪除了相機並忘記將標籤添加到新標籤,或者所有帶有該標籤的相機都被禁用。

+0

感謝ddk。我無法將相機連接到我的物體上。 – 11111