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 ();
}
然后错误就解决了,现在看来是一个很低级的错误,但当时造成了很大的困惑;这确实属于初学者踩坑
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 ();
}
然后错误就解决了,现在看来是一个很低级的错误,但当时造成了很大的困惑;这确实属于初学者踩坑
相关文章推荐
- SQL Select语句完整的执行顺序
- SQLServer中SELECT语句的执行顺序
- ASP脚本的执行顺序详细说明
- 数据库中的SELECT语句逻辑执行顺序分析
- 浅谈JavaScript 的执行顺序
- 探析浏览器执行JavaScript脚本加载与代码执行顺序
- Javascript代码在页面加载时的执行顺序介绍
- JavaScript 程序执行顺序问题总结
- For循环中分号隔开的3部分的执行顺序探讨
- ruby 程序的执行顺序
- Shell 命令执行顺序分析[图]
- JavaScript 全面解析各种浏览器网页中的JS 执行顺序
- Html中JS脚本执行顺序简单举例说明
- oracle select执行顺序的详解
- 在Java中自由块的执行顺序
- try catch finally的执行顺序深入分析
- Unity3D中脚本的执行顺序和编译顺序
- linux shell命令执行顺序的控制方法
- JavaScript执行顺序详细介绍
- 解读IE和firefox下JScript和HREF的执行顺序