空引用异常:对象引用未设置为对象的实例

时间:2015-06-03 23:50:08

标签: c# unity3d nullreferenceexception

我只是想在我正在尝试编码的游戏中寻求NullReferenceException错误的帮助。这是我第二次使用Unity,所以我对它的工作方式并不熟悉。

错误消息发送给我的地方(遗漏了不必要的代码):

     public GameObject TilePrefab;
public GameObject PlayerPrefab;

public int mapSize = 11;
public List<List<Tile>> map = new List<List<Tile>>();
public List<PlayerControl> players;
bool isP1Turn = true;


void Awake(){

    gObject = this;
}
void Start () {

    generatePlayers ();
    generateMap ();

}

void Update () {
    if (isP1Turn) 
        players [0].turnUpdate ();      
    else
        players [1].turnUpdate ();
}
   //Right Here
public void nextTurn(){
    if (isP1Turn) {
        isP1Turn = false;
    } else {
        isP1Turn = true;
    }
}

public void movePlayerChar(Tile t, int i){
    /*if (t.gridPosition.x - players[0].GetComponent<int >() > 0 || t.gridPosition.y - UserControl.player.moveRange > 0) {
    players[i].moveDestination = t.transform.position + 1.5f * Vector3.up;
}

void generatePlayers(){
    players = new List<PlayerControl> ();

    players.Add(((GameObject)Instantiate(PlayerPrefab, new Vector3(0 - Mathf.Floor(mapSize/2),1.5f, 0 + Mathf.Floor(mapSize/2)), Quaternion.Euler(new Vector3()))).GetComponent<PlayerControl>());
    players.Add(((GameObject)Instantiate(PlayerPrefab, new Vector3((mapSize-1) - Mathf.Floor(mapSize/2),1.5f, -(mapSize-1) + Mathf.Floor(mapSize/2)), Quaternion.Euler(new Vector3()))).GetComponent<PlayerControl>());


}

但我认为错误源于此处,在PlayerControl类中:

    public float mSpeed = 10.0f;
public Vector3 moveDestination;

void Start () {
    moveDestination = transform.position;

    transform.GetComponent<Renderer>().material.color = Color.blue;
}

void Update () {

}

void OnMouseEnter(){
    transform.GetComponent<Renderer>().material.color = Color.cyan;

    Debug.Log ("The cursor is on one of your units.");
}

void OnMouseExit(){
    transform.GetComponent<Renderer> ().material.color = Color.blue;

}

public void turnUpdate(){

    if (Vector3.Distance (moveDestination, transform.position) > 0.1f) {
        transform.position += (moveDestination - transform.position).normalized * mSpeed * Time.deltaTime;

        if(Vector3.Distance(moveDestination, transform.position) <= 0.1f){
            transform.position = moveDestination;
            GameManager.gObject.nextTurn();

        }
    }

}

我也相信我的'Tile'课程中的这种方法是相关的:

    void OnMouseDown(){
    if (GameManager.gObject.getTurn ()) {
        GameManager.gObject.movePlayerChar (this, 0);
        GameManager.gObject.nextTurn ();
    }else{
        GameManager.gObject.movePlayerChar(this, 1);
        GameManager.gObject.nextTurn();
    }

}

0 个答案:

没有答案