static vs getcomponent

What is the difference between using public static value and actually looking for a certain gameobject and using getcomponent to access the code, and reading the value like that? which is more power efficient?

eg:

public static int life= 1;

vs

public int life=1;
gameObj=GameObject.Find(“Player”);
playerScript=gameObj.GetComponent();
life= playerScript.life;

Finds and GetComponents are slow, Unity has to iterate through all objects to Find one, then search through each of that object’s components. If done often they can noticeably decrease framerate. Always avoid them if possible, and if you can’t then cache the results so it’s only done once.

Not sure if statics have any overhead vs local storage, but gotta be lots better than searching for it. Personally I either set the script reference via Inspector, or store a direct script reference during Start() and use that later.

If a script needs to be found in runtime by another, and it won’t have multiple of that type (like GameControllers), I tend to have it store a reference to itself in a static variable. Other scripts can then get that instance and store it locally.

public class ControllerGame : MonoBehaviour
{
  public static ControllerGame instance;

  void Awake()
  {
    if(instance == null)
    {
      instance = this;
      DontDestroyOnLoad(gameObject);
    }else if(instance != this)
    {
      Destroy(gameObject);
    }
  }
}

public class OtherClass : MonoBehaviour
{
  GameController gameController;
  void Start()
  {
    gameController = GameController.instance;
  }
}