I am using an object pool of pre-instantiated, inactive GameObjects with seeker components attached. I’d like the GameObject to calculate it’s path directly after I set it to active.
I want to do this, but I never get a callback to OnPathComplete:
void OnEnable() {
seeker.StartPath(transform.position, targetPosition, OnPathComplete);
}
Calling StartPath() slightly later, however, works:
void OnEnable() {
Invoke("InvokedFunction", 0.1f);
}
void InvokedFunction() {
seeker.StartPath(transform.position, targetPosition, OnPathComplete);
}
Any ideas what I’m doing wrong?
Thanks
Hi
The graphs are calculated during Awake. So in OnEnable the graphs might not exist since your component’s OnEnable might have been called before the AstarPath component’s Awake method has been called.
If you look in the console you will likely see an error message.
I recommend that you search for paths during Start.
If this is not right at the start of the scene, it might be that your script’s OnEnable call is called before the Seeker’s Awake call which the Seeker uses to set up some information (like callbacks). As previous, the solution is to request the path during Start.
Thanks you for the fast reply. It does look like the case was the seeker’s Awake() was not called before the other scripts OnEnable(). Swapping the order of the script components on the gameobject has fixed this.