Pathscript does not work on copies of object

I have pathfinding working great on my enemy object. The pathscript, seeker, and target are all set. When the enemy is hit, it draws a path and moves towards the the target (the player) flawlessly.

However, if I make a copy of the enemy object it does not behave the same. If I shoot the copy, it does not draw a path to the player, but if I shoot the original enemy it will.

Any idea what would cause this behavior?

Hi

Are you sure the references on the copied object are correct? It’s hard to debug without your code.

Yes, as far as I can tell it updated all references correctly

The code to make him run towards the player is below

	void Charge(){
		animator.SetBool("isRunning", true);
		pathscript.enabled = true;
		animator.SetBool("isAlerted", true);
		vectorToTarget = Player.transform.position - transform.position;
                vectorToTarget.Normalize();
                float rot_z = Mathf.Atan2(vectorToTarget.y, vectorToTarget.x) * Mathf.Rad2Deg;
                transform.rotation = Quaternion.Euler(0f, 0f, rot_z - 260);
		seeker.StartPath(transform.position, Player.transform.position, OnPathComplete);
	}

Is there anything out of the ordinary in the screenshot/code?

I can’t see anything wrong in the screenshot other than that the “pathscript” field is not even filled in.

The pathscript field populates correctly when pathscript.enabled is set to true.

Is there another way to enable the pathscript?

Should a copied object not share the same script as the original?

Hi

That field will not automatically be set to something when you set the enable property on it. You must be assigning it somewhere else.
No, each agent should have its own AIPath component.

Figured it out. I was starting the enemy charge by enabling the AIPath script, but should have been enabling the AI Destination Setter script.

Thanks for your help.

2 Likes