AI Instantiate targets/prefab. (pick target issue)

Ok so this time I have my AI instantiate an empty GameObject(prefab) at the hit point of one of the rays and then make that its target to follow/go towards.

The problem is that it doesn’t select the prefabbed instantiated target but rather the original in he middle of the scene.

So yeah what do I need to do in order get it to pick the target which the AI insantiates.


// This is whatever the AI targets.

public Transform target;
// This is the prefabbed target object that is instantiated.

public Transform targetDummy;

//and this where the magic is supposed to happen lol.

if(Physics.Raycast(headdummyBone.position, fwd, out hit, maxDist, ignoreLayers)){

   if(startWandering == true && dummyTarget01Set == false){

      Instantiate(targetDummy, hit.point, transform.rotation);

//this is where the target is set.
target = targetDummy.transform;

dummyTarget01Set = true;



Set it to the result of the Instantiate ( C# code ):

GameObject resultingInstance = (GameObject)Instantiate(targetDummy, hit.point, transform.rotation);
target = resultingInstance.transform;