Question

I am working on a small test project where 99.9% of the game is in the code and 0.1% is with the visual editor.

I am creating tile map for some sort of Tower Defence. each map is 5/5 and are loaded from a text file in the resources folder

i dont understand why but it only work 1 time. if i try to build 2 map or build 1, destroy it and rebuild it i get this error:

NullReferenceException: Object reference not set to an instance of an object MapBuilder.buildMap (UnityEngine.GameObject parent, .Map map) (at >Assets/script/MapBuilder.cs:21) Map.build (System.String _name) (at Assets/script/Map.cs:39) Main.Start () (at Assets/script/Main.cs:20)

my main class (only creating maps for test at the moment)

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

}

and here is the part where i have the bug

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

the error is at this exact line: reader = new StringReader(mapDataStream.text);

It ask a lot more knowledge to do the game without using the editor and i learned a lot so far. But this is the first bug i did not find a solution by myself

this is what a map look like

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

(need 10 reputation to inline the image)

i get the data from a text file who look like this: 00000 11111 00000 11011 11111

and i create the mesh myself (primitive plane have 2 triangle per unit and modifying them would be to much for nothing, mine have 2 triangles)

there is no texture so far. this will be added later

the player will be able to build his own maps to defend. He will be able to add a set number of maps of many form and he will be able to rotate them and etc... the next step will be to add the pathfinding to validate the map set by the player.

thanks for your help

Was it helpful?

Solution

If you check the following line:

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

By debugging your code; step through the code line by line. Determine if mapDataStream is ever NULL because if it is, the next line of code will throw a NullReferenceException because you attempt to read a property of a null object.

In Mono you can follow their guide for debugging to easily step through your code and identify problems. Refer to this link https://docs.unity3d.com/Documentation/Manual/Debugger.html

Its as simple as putting a break point on the line you wish to debug:

  1. Open your project in Mono Develop.
  2. hit play in the Unity Editor.
  3. In Mono Develop select run attach to process... and select unity editor.
  4. You can now use breakpoints in Mono Develop and step through code, watch variables, etc.

Here is a video to help you understand this better:

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top