[SOLVED] What to do instead of GameObject.Find("Somethingmoving").transform every frame?

The thing i’m doing right now is casting a line from one object to another in real time, every frame, for which i need the location of the target object also in real time.

The problem is I’ve been reading a lot recently on how GameObject.find is really slow, so I don’t want it running every frame.

private Transform player_transform;

void Update (){
player_transform = GameObject.Find("Player").transform;
if(Physics.Linecast(transform.position, player_transform.position))
{
//do the thing with linecast, not relevant to question
}
}

So my question is, what to do instead?

BTW the script will be attached to a prefab, so any variable inspector assigning stuff won’t work.

player_transform = GameObject.Find(“Player”).transform;

Just move that line into the Start() method. As this just stores a reference and not the actual data, you will always get the current position in Update().