[Solved]Problems recalculating the path after instantiated object gets destroyed

Greetings,

I have an issue related to recalculating the path after an instantiated object gets destroyed. I’m trying to make a 2D Tower Defense game and i’m using the A* pathfinding for the enemies to find their way around obstacles.

So far everything works great, i can place obstacles and the units recalculate the path and avoid them, but when one of the units die the rest stop recalculating the path and when i place a new obstacle i get a “MissingReferenceException” pointing to the recalculation and to be more precise to the transform.position inside the seeker.StartPath.

public void RecalculatePath(AstarPath ap)
{
seeker.StartPath(transform.position, target.position, OnPathComplete);
}

I’ve been searching for days trying to find why i get a Missing Reference Exception inside the recalculation and trying everything i could think of to fix it. I only found today that the actual problem is the transform.position (first i thought it had to do with StartPath and then with the script itself being destroyed).

I’m instantiating the object using using a different object (i call it Spawner) and at the same time i assign the AstarAI script to it.
Code:

public GameObject monsterToSpawn;
private GameObject spawnMonster;

void Update()
{
    if (Input.GetKeyDown("space"))
    {
        spawnMonster = Instantiate(monsterToSpawn, transform.position, Quaternion.identity) as GameObject;
       
        spawnMonster.AddComponent <*AstarAI*>();
        
    }

    if(Input.GetKeyDown("d"))
    {
        Destroy(spawnMonster);
    }
}

I don’t think posting the AI scripted is needed since it’s taken from the example and it’s pretty much the same with a few changes/additions like recalculating the path onEnable.

public void OnEnable()
{
    AstarPath.OnGraphsUpdated += RecalculatePath;
}

public void RecalculatePath(AstarPath ap)
{      
    seeker.StartPath(transform.position, target.position, OnPathComplete);  
}

At this point i don’t know if i should be asking this here or somewhere else as i’m not sure if it is related to pathfinding or if its a game logic error. Is there a way to get the objects position in a different way that i don’t know/can think of? Is my logic wrong here?

I should say that if instead of “transform.position” if i use a predefined Vector3 it works fine without any problems, but as you can imagine every time the recalculation happens the unit moves back to that predefined position.

I posted this question a while ago on unitys answer forums (here) but i got no answer.

Any help appreciated, also if someone knows where i can find a guide for 2D Tower Defense game logic (preferably with AI involved) please post the link or send me a pm.

Thanks.

Hi

In the OnEnable method you register to the OnGraphsUpdated delegate. This is all fine, however when that component is destroyed, that doesn’t make it stop receiving those events. So when the graph is updated the next time, it will call RecalculatePath on a destroyed object which is why you get the missing reference exception. To solve this, add the method OnDisable and unregister from the delegate there.

public void OnDisable () {
     AstarPath.OnGraphsUpdated -= RecalculatePath;
}
1 Like

It worked! I’ve been trying to find a solution for so many days now.

Thanks a lot!