我无法创建一个游戏对象阵列

时间:2014-09-11 17:56:24

标签: c# arrays unity3d gameobject

我想为我的游戏制作一个GameObject数组。但我无法做到,因为我收到了错误。以下是我的代码:

using UnityEngine;
using System.Collections;

public class TestArray : MonoBehaviour {
    public class Tanah
    {
        public static string id;
        public static string name;
        public static GameObject objTanah;

        public void setObjTanah(Transform pref,float X,float Y)
        {
            objTanah = GameObject.Instantiate(pref,new Vector3(X,Y,0),Quaternion.identity)as GameObject;
        }

        public void setId(string objId)
        {
            id = objId;
        }
        public void setName(string objName)
        {
            name = objName;
        }
        public Tanah()
        {
            objTanah = null;
            name = null;
            id = null;
        }
    };

    public GameObject lahan;
    public int jumLahan = 10;
    public int rows = 10;
    public int columns = 10;
    public float mapX;
    public float mapY;
    public Transform prefab;
    public Tanah[,] myLahan;

    // Use this for initialization
    void Start () {
        myLahan = new Tanah[rows, columns];
        //lahan = new GameObject[rows, columns];
        for (int i = 0; i < rows; i++) {
            for(int j = 0 ; j<columns;j++)
            {
                mapX = (i-j)*0.8f;
                mapY = (i+j)*0.4f;
                //myLahan[i,j].objTanah = GameObject.Instantiate(prefab,new Vector3(mapX,mapY,0),Quaternion.identity)as GameObject;
                //lahan = GameObject.Instantiate(prefab,new Vector3(mapX,mapY,0),Quaternion.identity)as GameObject;
                myLahan[i,j].setObjTanah(prefab,mapX,mapY);
                if( i==0 || i>=5 || j==0 || j>=5)
                {
                    prefab.name = "snow-"+i+"-"+j;
                }
                else 
                {
                    prefab.name = "tanah-"+i+"-"+j;
                }
                //myLahan[i,j].setName(prefab.name);
                Debug.Log("buat kotak");
            }
        }
    }


    // Update is called once per frame
    void Update () {

    }
}

错误为"Object reference not set to an instance of an object"

0 个答案:

没有答案