您的位置:首页 > 移动开发 > Unity3D

Unity学习踩坑

2016-03-22 10:13 411 查看
在以下代码中,InitMap()为生成随机地图的方法,其中调用到了挂载在同一个GameObject上的GameManager脚本。脚本运行后一直报错,查看错误为生成障碍物的wallCount没有值。

using UnityEngine;
using System.Collections.Generic;

public class MapManager : MonoBehaviour {

public GameObject[] OutWallArray;
public GameObject[] FloorArray;
public GameObject[] wallArray;
public GameObject[] foodArray;
public GameObject[] enemyArray;
public GameObject ExitPrefab;

public int rows=10;
public int cols=10;

public int MinWallCount = 2;
public int MaxWallCount = 8;

private Transform MapHolder;
private List<Vector2> positionList = new List<Vector2>();

private GameManager gameManager;

// Use this for initialization
void Awake () {
InitMap ();
gameManager = this.GetComponent<GameManager> ();
}

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

}

//初始化地图
private void InitMap(){
MapHolder = new GameObject ("Map").transform;

for (int x = 0; x<cols; x++) {
for (int y=0;y<rows;y++){
if(x==0||y==0||x==cols-1||y==rows-1){
GameObject outwallPrefab = RandomPrefab(OutWallArray);
GameObject go1 = GameObject.Instantiate(outwallPrefab,new Vector3(x,y,0),Quaternion.identity) as GameObject;
go1.transform.SetParent(MapHolder);
}

else{
GameObject floorPrefab = RandomPrefab(FloorArray);
GameObject go1 = GameObject.Instantiate(floorPrefab,new Vector3(x,y,0),Quaternion.identity) as GameObject;
go1.transform.SetParent(MapHolder);
}
}
}
positionList.Clear ();
for (int x = 2; x< rows-2; x++) {
for(int y = 2;y <cols-2;y++){
positionList.Add(new Vector2(x,y));
}
}

//生成障碍物
int wallCount = Random.Range (MinWallCount, MaxWallCount);
InstantiateItemPrefabs (wallCount, wallArray);

//生成食物2-level*2
int foodCount = Random.Range (2, gameManager.level * 2 + 1);
InstantiateItemPrefabs (foodCount, foodArray);

//生成敌人level/2
int enemyCount = gameManager.level / 2;
InstantiateItemPrefabs (enemyCount, enemyArray);

//生成exit
GameObject go2 = Instantiate (ExitPrefab, new Vector2 (rows - 2, cols - 2), Quaternion.identity) as GameObject;
go2.transform.SetParent (MapHolder);

}

private GameObject RandomPrefab(GameObject[] Prefabs){
int index = Random.Range (0, Prefabs.Length);
return Prefabs [index];
}

private Vector2 RandomPosition(){
int positionIndex = Random.Range (0, positionList.Count);
Vector2 pos = positionList [positionIndex];
positionList.RemoveAt (positionIndex);
return pos;
}

private void InstantiateItemPrefabs(int itemCount,GameObject[] prefabsArray){
for (int i=0; i<itemCount; i++) {
Vector2 pos = RandomPosition();
GameObject ItemPrefab = RandomPrefab(prefabsArray);
GameObject go = Instantiate(ItemPrefab,pos,Quaternion.identity)as GameObject;
go.transform.SetParent(MapHolder);
}
}
}
又看了一下代码,发现问题在如下代码:
void Awake () {
InitMap ();
gameManager = this.GetComponent<GameManager> ();
}
代码中先执行了InitMap动作,而InitMap中的生成障碍物用到了GameManager脚本中的public变量level,但这个时候还没有取得GameManager组件,导致了报错。
然后对两句代码的顺序进行了修改,如下:

void Awake () {
gameManager = this.GetComponent<GameManager> ();
InitMap ();
}

然后错误就解决了,现在看来是一个很低级的错误,但当时造成了很大的困惑;这确实属于初学者踩坑
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  unity 执行顺序