Support Forum

How to set target when spawning

Hello, im very new to both A* and unity, and when i save my prefab of the enemy that has the AI path and ai destination setter, the target transfor just disapears. So i wold like to ask for help on how to set the target when it spawns. Thanks in advance, and also, i have added using pathfinding but when i write GetComponent(); it says
Error CS0246 The type or namespace name ‘AIPath’ could not be found (are you missing a using directive or an assembly reference?)

This is more of Unity thing than Astar.
Here’s my 2 cents (I’m self taught so you might find my words not very technically correct):

  • Unity: a link to a scene object (when you drag that object on scene into AIDestination Setter) will not survive when it becomes a prefab. If you take a look, it’ll become “Mismatched type” or s.t. That’s why when a prefab spawned, it does not know its target (whose link is now destroyed).
  • You can:
  1. Set target right when enemies spawned. Look up tutorial for that, very simple and beginner friendly.
  2. Set target a Scriptable Object which will survive anything. This is still simple, but would take more time to learn depending on how your current skill is.
    Best of luck,

Thanks for reply, and how do I do the 1. option (the bigginer frendly)?

Something like

void Start () {
    var go = GameObject.Instantiate(...);
    go.GetComponent<AIDestinationSetter>().target = myPlayerObject;
}