#recusion

1 messages · Page 1 of 1 (latest)

autumn siren
#

now this is all working as expected and i can get the gameobject of each end tile with debug.log

now when i add in
PathManager.instance.paths.Add(newPath);

i get an error "object reference not set to an instance of an object"

i've been fumbling with this for a few hours now and wondering if anyone can point me in the right direction

public class Path 
{
    public List<BaseTile> tiles = new List<BaseTile>();

    public Path(List<BaseTile> tiles = null)
    {
        if(tiles != null)
        {
            foreach (var tile in tiles)
            {
                this.tiles.Add(tile);
            }
        }
    }

    public BaseTile getPreviousTile()
    {
        if (tiles.Count > 0)
        {
            return tiles.Last();
        }
        else
            return null;
    }

}
#

this is the path class

#
    public void createAllPossiblePaths()
    {
        if (canMove)
        {
            PathCreator pathCreator = new PathCreator();

            pathCreator.Iterate(currentTile, new Path(), roll);

        }


    }

this happens on player input to start up the recursive function

#
public class PathManager : MonoBehaviour
{

    public static PathManager instance;

    public List<Path> paths;

    private void Start()
    {
        instance = this;
    }

    public void clearAllPaths()
    {
        if(paths.Count > 0)
        {
            instance.paths.Clear();
        }

    }





}

and this is my static class where i am trying to edit the list

#

additionally every list after the first list is duplicating for some reason

#

first list is fine.... with a roll of 3

BaseTile(5) gameobject is our destination
newPath[0] = BaseTile (current tile)
newPath[1] = BaseTile1
newPath[2] = BaseTile2 (tile right before destination)
newPath[3] = BaseTile5 ( our destination)

then the next list

BaseTile(4) gameobject is our destination
newPath[0] = BaseTile (current tile)
newPath[1] = BaseTile1
newPath[2]= BaseTile1 - so now we have a duplicate of the same tile in the list
newPath[3]= BaseTile3 (tile right before destination)

this second list SHOULD look like this

BaseTile(4) gameobject is our destination
newPath[0] = BaseTile (current tile)
newPath[1] = BaseTile1
newPath[2]= BaseTile3 (tile right before destination)
newPath[3]= BaseTile4 ( our destination)

and for whatever reason the PathManager.instance.Add(newPath) still throws an object reference error every time

lone pumice
#

I assume you meant PathManager.instance.paths.Add(newPath), which will nullref because you never created PathManager.instance.paths

autumn siren
#

because instance is a static variable

#

and paths is a variable of PathManager

lone pumice
#

paths isn't a static variable, and even if it were it's still null. So accessing it is going to give you a nullref