2016-10-27 103 views
0

我把一個簡單的腳本,創建一個我可以在UI中使用的基於精靈的指針。它尚未完成(當玩家按下按鈕時仍需要支持),但已經遇到問題。GameObject被設置爲空後

我得到一個NullReferenceException:未將對象引用設置到對象的實例

在52行...

_ptrSpriteRenderer.sprite = newSprite; 

... 72

_ptr.transform.position = new Vector2(((float)_ptrPosX * _ptrPositionXDistance) + _ptrPositionTopLeft.x, ((float)_ptrPosY * _ptrPositionYDistance) + _ptrPositionTopLeft.x); 

我在Start中創建_ptr和_ptrSpriteRenderer,但由於某些原因,在我所有其他函數中,這兩個GameObjects都爲null,但在Start期間它們不爲null。

我確定這是一件很簡單的事情,但我花了數小時將這個類與其他創建子類的類進行比較,但我看不到問題。

using UnityEngine; 
using System.Collections; 

public class Pointer : MonoBehaviour { 

    private GameObject _ptr; 
    private Sprite _ptrSprite; 
    private SpriteRenderer _ptrSpriteRenderer; 

    private bool _ptrEnabled;   // Is the pointer receiving input? 
    private int _ptrMovePerSecond;  // Number of positions to move per second if input held down 
    private int _ptrXInput; 
    private int _ptrYInput; 
    private float _ptrTimeSinceLastInput = 0f; 
    private float _ptrTimePerInput = 0.25f; 
    private bool _ptrInputDelay = false; 

    private Vector2 _ptrPositionTopLeft; //The top left position the pointer can reach in the grid 
    private Vector2 _ptrPositionBottomRight; //The bottom right position the pointer can reach in the grid 
    private int _ptrPositionsX; //The number of grid positions the pointer can traverse in X 
    private int _ptrPositionsY; //The number of grid positions the pointer can traverse in Y 
    private float _ptrPositionXDistance; //The distance of each X position in the grid 
    private float _ptrPositionYDistance; //The distance of each Y position in the grid 

    private int _ptrPosX; //Current X position of pointer in the grid 
    private int _ptrPosY; //Current Y position of pointer in the grid 



    // Use this for initialization 
    void Start() { 
     _ptr = new GameObject(); 

     _ptrSpriteRenderer = new SpriteRenderer(); 
     _ptr.AddComponent<SpriteRenderer>(); 
     _ptrSpriteRenderer = _ptr.GetComponent<SpriteRenderer>(); 

     _ptrEnabled = true; 


    } 

    public void setSprite (Sprite newSprite) 
    { 

     if (newSprite == null) 
     { 
      Debug.LogError("No sprite passed to setSprite in Pointer"); 
     } 
     else 
     { 
      _ptrSpriteRenderer.sprite = newSprite; 
     } 
    } 

    public void setPositions (Vector2 positionTopLeft, Vector2 positionBottomRight, int numPositionsX, int numPositionsY) 
    { 
     _ptrPositionsX = numPositionsX; 
     _ptrPositionsY = numPositionsY; 
     _ptrPositionTopLeft = positionTopLeft; 
     _ptrPositionBottomRight = positionBottomRight; 

     _ptrPositionXDistance = Mathf.Abs((positionBottomRight.x - positionTopLeft.x)/numPositionsX); 
     _ptrPositionYDistance = Mathf.Abs((positionBottomRight.y - positionTopLeft.y)/numPositionsY); 
    } 

    public void setPosition (int x, int y) 
    { 
     _ptrPosX = x; 
     _ptrPosY = y; 

     _ptr.transform.position = new Vector2(((float)_ptrPosX * _ptrPositionXDistance) + _ptrPositionTopLeft.x, ((float)_ptrPosY * _ptrPositionYDistance) + _ptrPositionTopLeft.x); 
    } 



    // Update is called once per frame 
    void Update() { 
     //Is the pointer enabled? 

     if (_ptrEnabled) 
     { 
      if (_ptrInputDelay) 
      { 
       _ptrTimeSinceLastInput += Time.deltaTime; 
       if (_ptrTimeSinceLastInput >= _ptrTimePerInput) 
       { 
        _ptrInputDelay = false; 
       } 
      } 

      if (_ptrInputDelay == false) 
      { 
       _ptrXInput = (int)Input.GetAxis("Horizontal"); 
       _ptrYInput = (int)Input.GetAxis("Vertical"); 

       if (_ptrXInput != 0 || _ptrYInput != 0) 
       { 
        _ptrPosX += _ptrXInput; 
        _ptrPosY += _ptrYInput; 

        Debug.Log("WHEE"); 

        if (_ptrPosX < 0) _ptrPosX = 0; 
        if (_ptrPosX > _ptrPositionsX) _ptrPosX = _ptrPositionsX; 
        if (_ptrPosY < 0) _ptrPosY = 0; 
        if (_ptrPosY > _ptrPositionsY) _ptrPosY = _ptrPositionsY; 

        _ptr.transform.position = new Vector2(((float)_ptrPosX * _ptrPositionXDistance) + _ptrPositionTopLeft.x, ((float)_ptrPosY * _ptrPositionYDistance) + _ptrPositionTopLeft.x); 

        _ptrInputDelay = true; 
        _ptrTimeSinceLastInput = 0f; 
       } 
      } 
     } 
    } 
} 

而且在我的指針類被稱爲地方是這樣做:

GameObject newPointer = new GameObject(); 
    newPointer.AddComponent<Pointer>(); 
    Pointer newPointerScript = newPointer.GetComponent<Pointer>(); 
    newPointerScript.setPositions(new Vector2(-1f, -1f), new Vector2(1f, 1f), 3, 3); 
    newPointerScript.setSprite(newWeapon); 
    newPointerScript.setPosition(1, 1); 

回答

1

這些線條看上去是錯誤的:

_ptrSpriteRenderer = new SpriteRenderer(); 
    _ptr.AddComponent<SpriteRenderer>(); 
    _ptrSpriteRenderer = _ptr.GetComponent<SpriteRenderer>(); 

您正在創建一個SpriteRenderer,然後兩條線稍後用_ptr中的值覆蓋該值,這很可能是空值。

你真的需要這條線嗎?

另外,如果你添加一個組件,你不應該實際上將組件傳入Add方法?

0

因此,如果我用Awake()切換Start(),那麼一切正常。這就是所需要的。

相關問題