How to implement automatic ingame script rewriting

Is it possible to have the script of a Gameobject’s component automatically rewritten during the game ? For example, a unit is at place A and its component’s script contains the path to go to place B. Once B is reached by the unit, the script is replaced by another script that contains the path to go to C.

You could remove the component that moves the game object from A to B when it reaches B and then Add the second script that moves the game object from B to c using

Destroy(scriptA) ;

However if you’re going to be recycling these id just enable one and disable the other.

Or better yet have instances of both classes that derive from a base class with virtual methods and switch bewteen the current instance like a simple state machine.