Null Scripts

In an attempt to have some form of AStar pathfinding I have my enemies each referencing an AStar script, however, no matter how I initialise it, the referenced script always seems to be null, I have tried attaching it to the prefab and using:

AStar aStar = getComponent();

I’ve also tried

Astar astar = new AStar();

but no matter what I do, the variable remains null, it’s infuriating because I swear I’ve done this before, but it just doesn’t seem to be working this time, can anyone help me out here? Am I doing something stupid?

I’m using C# by the way.

If the script is attached to the same object, you should use:

AStar astar = GetComponent();

But if the script is attached to another object, you need some reference to it. Supposing other is the object transform or collider:

AStar astar = other.GetComponent();