[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.
Answer by doublemax · Sep 03, 2016 at 10:28 AM
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().
Oh, I've always thought it would just get the position one time and don't try doing it again. Thanks!
Your answer
![](https://koobas.hobune.stream/wayback/20220612092108im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Different jump height 1 Answer
Transform.translate -> Update or FixedUpdate 1 Answer
Changing transform.localscale doesn't take affect immediately? The next RayCast isn't precise 1 Answer
Transform not updating 0 Answers
Trying to switch two moving character's positions. Only working sometimes. 0 Answers