Given a field that derives from Component in a Unity class, e.g. MonoBehaviour, add a context action to initialise the value in the Start() method, in a similar way that current Rider/ReSharper will add code to initialise in the constructor. E.g. Given
public class MyClass : MonoBehaviour {
private RigidBody _rigidBody;
}
Then there should be a quick fix on _rigidBody that will generate:
public class MyClass : MonoBehaviour {
private RigidBody _rigidBody;
}
public void Start() {
_rigidBody = GetComponent<RigidBody>();
}
would there be a option to choose, if it ends up in Start or Awake?
馃憤 to option for Start vs Awake. As I've said before (#107), Awake is probably the better choice for this specific case especially.
General rule that seems to be roughly used: Awake for self-setup, Start when you need to observe other actors.
(OT: When I doodle in Unity now, I tend to have a setup in which I [SerializeField] private RigidBody rigidbody, (finagle to avoid shadowing deprecated autos,) and then in-editor-only GetComponent<RigidBody> in Awake and scream if it's not the same thing. Very rough benchmarks and reading around more than a year ago now gave me the impression that sticking to the deserialization process was better than hitting GetComponent.)
Yep, this sounds like a good idea - two menu items, add to Awake(), add second to Start() (there's already an option to mark with [SerializeField], and we'll aim to make that first).
Not sure what the difference is between "self-setup" and "observing", though - ownership?
While in Start you know that everything has called it's Awake already and it's safe to Start interacting with other objects.
Awake would be the best place for this feature since is what should be used to setup stuff internal to your object and create references.
See also RIDER-15621
See also #608
Closed with #1259
Most helpful comment
While in Start you know that everything has called it's Awake already and it's safe to Start interacting with other objects.
Awake would be the best place for this feature since is what should be used to setup stuff internal to your object and create references.