字符串读取器只工作一次

本文关键字:一次 工作 读取 字符串 | 更新日期: 2023-09-27 17:53:03

我正在进行一个小型测试项目,其中99.9%的游戏是在代码中,0.1%是在视觉编辑器中。

我正在为某种塔防创建贴图。每个地图是5/5,从资源文件夹

中的文本文件中加载。

我不明白为什么,但它只工作一次。如果我尝试构建2张地图或构建1张地图,破坏它并重建它,我会得到这个错误:

NullReferenceException:对象引用未设置为对象的实例MapBuilder。buildMap (UnityEngine。GameObject parent, .Map map) (>Assets/script/MapBuilder.cs:21)地图。构建(系统。字符串_name)(在Assets/script/Map.cs:39)主要。Start () (at 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 () {
}
}

my map class

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();

错误就在这一行:

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

这就是地图的样子

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

(需要10个声誉来内联图像)

我从一个文本文件谁看起来像这样的数据:0000011111000001101111111

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

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

玩家将能够建立自己的地图来防御。他将能够添加一系列多种形式的地图,他将能够旋转它们等等……下一步将是添加寻径来验证玩家设置的地图。

谢谢你的帮助

字符串读取器只工作一次

如果检查以下行:

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

通过调试代码;逐行逐步浏览代码。确定mapDataStream是否为NULL,因为如果是,下一行代码将抛出NullReferenceException,因为您试图读取null对象的属性。

在Mono中,您可以遵循他们的调试指南,轻松地逐步完成代码并识别问题。参考此链接https://docs.unity3d.com/Documentation/Manual/Debugger.html

就像在你想调试的行上加一个断点一样简单:

  1. 在Mono Develop中打开你的项目。在Unity编辑器中点击播放。
  2. 在单声道开发选择运行附加到进程…然后选择unity编辑器。
  3. 你现在可以使用断点在Mono开发和步进代码,观察变量等。

这里有一段视频可以帮助你更好地理解这一点:

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