- Home /
Pathfinding script breaking for prefabs only
OK so I've had a really annoying problem for the last three days and I've finally nailed down the problem but not sure how to fix it. I have used a youtube tutorial series by Sebastain Lague on creating your own path-finding which works amazingly well and I wanted to create my own target selection to find the closest target and set that as the target for the path-finding to use which i have set up and works fine. I than created a Spawner script but any enemy created through this script the path-finding will only work for the first target it finds to be the closest if the player or another object becomes the closest it selects that as the target but refuses to move towards it or use path-finding at all.
So my question is simple why does this work for objects that ain't prefabs. I myself can't imagine something working fine for just a gameobject in the level at the start and one that I spawned in during the game. This is the part that is the confusing to me as all the objects are the same and if i just duplicate an enemy it will work fine but instantiate one in and i get an error. The only clue I have is an error which shows the following:
If you want to see the spawner script and other scripts affected I have a unity forums thread with them all shown listed below:
https://forum.unity.com/threads/dynamic-path-finding-script-having-problems.549877/
So completely forgot to thank anyone who can help me with this problem thank you for any help given :3.
that error means that in your code you are accessing a variable before it is filled. in your case i would guess your pathfinding script is trying to access a Gameobject variable before it is asigned to something.
make sure all your spawning scripts are finished running, before you run your pathfinding script.
How would go about doing that? sounds like a decent fix if i knew how to do it xD. I know you can change the order in which scripts are executed but does doing that only apply to scripts when the game is first built/started?
So let me phrase that better if I put the targetSelection script before the unit script would it would apply once or would targetSelection always be executed first even through the update function etc.
p.s. thanks for the reply I want to thank you for supporting me :3
edit: 2 had a brainwave just now.... could we also be talking a simple bool check that's set to false in till after the object is instantiated and switch it to true on the pathfinding script when finding a path?
@toddisarockstar I want to thank you I've now sorted the problem mostly it doesn't work as quite as intended but you put my on the right thought path so thank you very much. All of them are now dynamically switching targets whether they are prefabs or not. It was rather simple once you told me what the error was. So thank you for helping me out :3
Answer by telecaster · Sep 04, 2018 at 10:33 PM
Sorry for the dumb question, but is pooling an option? That way all targets are in the scene, but not active? Or maybe I am just missing the issue.
Wouldn't I still get an error as it would still become active like if i instantiated it or am I mis-understanding what pooling is? I know its to store gameobjects to reduce lag etc via having them in the world already but wouldn't I still get the error as pooling still needs to refresh the list or array of gameobjects or am I talking nonsense?
I also want to make it clear that the AI in the level work fine with no errors or anything it only applies to ones that are created through Instantiated objects so I'm not sure if pooling is a fix as I might still get the error but anything is worth a try at this point :3 so ill have a look into pooling when I get a chance to.