Like this:
private PlayerInput _playerInput => GetComponent();
private Camera _mainCamera => Camera.main;
Will using the those variables just constantly do use the GetComponent function and stuff, or is the reference cached when the code is compiled and the variable already has the reference ready?
Pretty sure using lambda expressions this way isn’t really any different from declaring it inside a method, so it will run GetComponent every time.
Can you? Yes. Does your code do that? No.
A cached reference in a property would look something like:
private Camera mainCamera {
get {
if (_mainCamera == null) _mainCamera = Camera.main;
return _mainCamera;
}
}
private Camera _mainCamera;
2 Likes
We recently had a similar thread here and I posted this solution .