Do we have to use GameObject.GetComponent("") every frame

Hi,

Do we have to use GameObject.GetComponent(“”) every frame to get the latest data or is it a pointer to the component?
Regards,
vanhouten777.

Hi,

Same way do we have to use GameObject.Find(“”) every frame to get the latest data of the game object or is it a pointer to the game object?
Regards,
vanhouten777.

if it’s for something that isn’t going to be changed do it in the start function and cache the result in a variable. It’s a pointer.

Hi LeftyRighty,

Thanks for the reply.
Regards,
vanhouten777.

Or if it’s not dynamic you can setup a public GameObject variable in the script that needs it set it in the editor.

Or you can set up a script on the Object with a static public use variable that records the gameobject on start which can then be accessed at any time.

class Singleton()
{
public static GameObject use;

public void Awake()
{
   use = gameObject;
}
}

Then you can just write Singleton.use to access this game object. This is best used where there is only one of these objects in your game, for example Manager classes or core Game components.