0

我正在做一个小型测试项目,其中 99.9% 的游戏在代码中,0.1% 在可视化编辑器中。

我正在为某种塔防创建瓷砖地图。每张地图都是 5/5,从资源文件夹中的文本文件加载

我不明白为什么,但它只工作 1 次。如果我尝试构建 2 地图或构建 1,将其销毁并重建它,我会收到此错误:

NullReferenceException:对象引用未设置为对象 MapBuilder.buildMap(UnityEngine.GameObject 父级,.Map 地图)的实例(位于 >Assets/script/MapBuilder.cs:21) Map.build(System.String _name)(位于 Assets /script/Map.cs:39) Main.Start () (在 Assets/script/Main.cs:20)

我的主要课程(目前仅创建用于测试的地图)

public class Main : MonoBehaviour {
GameObject mainObject;

Map map;
// Use this for initialization
void Start () {
    mainObject = GameObject.Find("Main");

    map = gameObject.AddComponent<Map>();

    map.build("map_start");

    GameObject map2 =  GameObject.Find("map_start1");

    Map map2C = map2.AddComponent<Map>();

    map2C.build("map_start1");
}

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

}
}

我的地图课

public class Map : MonoBehaviour {

public List<GameObject> planes;

public List<List<int>> mapData;

public string mapName;


public void build(string _name)
{
    mapName = _name;
    if(planes != null)
    {
        delete();
    }
    else
    {
        planes = new List<GameObject>();

        mapData = new List<List<int>>();
    }
    MapBuilder.buildMap(gameObject, gameObject.GetComponent<Map>());

}

private void delete()
{
    for(int i = 0; i < planes.Count; i++)
    {
        Destroy(planes[i]);
    }

    mapData.Clear(); //do not clear capacity! only clear element (avoid     reallocating the memory)

    planes = new List<GameObject>();

    mapData = new List<List<int>>();
}

}

这是我有错误的部分

    public const float height = -2;

public static Map buildMap(GameObject parent, Map map)
{       
    //get the stream reader ready
    FileInfo sourceFile = null;
    StringReader reader = null;

    Debug.Log(map.mapName);
    //load
    TextAsset mapDataStream = (TextAsset)Resources.Load(map.mapName,  typeof(TextAsset));
    //Debug.Log(mapDataStream.text);
    //read
    reader = new StringReader(mapDataStream.text);

    for(int x = 0; x < 5; x++)
    {
        string txt = reader.ReadLine();

        map.mapData.Add(new List<int>());

        //get height data
        for(int y = 0; y < 5; y++)
        {
            map.mapData[x].Add(0);
            map.mapData[x][y] = 49 - txt[y];

        }
    }

    mapDataStream = null;
    reader.Close();

错误就在这一行: reader = new StringReader(mapDataStream.text);

在不使用编辑器的情况下做游戏需要更多的知识,到目前为止我学到了很多。但这是我自己没有找到解决方案的第一个错误

这就是地图的样子

http://i.stack.imgur.com/ouGMr.png

(内联图像需要 10 个声望)

我从一个看起来像这样的文本文件中获取数据:00000 11111 00000 11011 11111

我自己创建了网格(原始平面每个单元有 2 个三角形,修改它们将是徒劳的,我的有 2 个三角形)

到目前为止还没有纹理。这将在稍后添加

玩家将能够建立自己的地图进行防御。他将能够添加一定数量的多种形式的地图,并且他将能够旋转它们等等……下一步将是添加寻路以验证玩家设置的地图。

感谢您的帮助

4

1 回答 1

0

如果您检查以下行:

TextAsset mapDataStream = (TextAsset)Resources.Load(map.mapName, typeof(TextAsset));

通过调试您的代码;逐行浏览代码。确定 if mapDataStreamis everNULL因为如果是,下一行代码将抛出 aNullReferenceException因为您尝试读取空对象的属性。

在 Mono 中,您可以按照他们的调试指南轻松单步调试代码并找出问题。请参阅此链接https://docs.unity3d.com/Documentation/Manual/Debugger.html

它就像在要调试的行上放置一个断点一样简单:

  1. 在 Mono Develop 中打开您的项目。
  2. 在 Unity 编辑器中点击播放。
  3. 在 Mono Develop 中选择 run attach to process... 并选择 unity editor。
  4. 您现在可以在 Mono Develop 中使用断点并单步执行代码、观察变量等。

这是一个视频,可以帮助您更好地理解这一点:

http://www.youtube.com/watch?v=-D6qXaGSC2k

于 2014-03-25T18:33:43.510 回答