Unity学习踩坑

在以下代码中,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 ();
	}

然后错误就解决了,现在看来是一个很低级的错误,但当时造成了很大的困惑;这确实属于初学者踩坑


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值