Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "Initialise Component in Start()" context action #356

Closed
citizenmatt opened this issue Feb 6, 2018 · 7 comments
Closed

Add "Initialise Component in Start()" context action #356

citizenmatt opened this issue Feb 6, 2018 · 7 comments
Assignees
Milestone

Comments

@citizenmatt
Copy link
Member

citizenmatt commented Feb 6, 2018

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>();
}
@citizenmatt citizenmatt added this to the Rider 2018.1 milestone Feb 6, 2018
@citizenmatt citizenmatt changed the title Add "Initialise Component in Start()" quick fix Add "Initialise Component in Start()" context action Feb 6, 2018
@cmcpasserby
Copy link
Contributor

would there be a option to choose, if it ends up in Start or Awake?

@CAD97
Copy link

CAD97 commented Feb 7, 2018

👍 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.)

@citizenmatt
Copy link
Member Author

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?

@cmcpasserby
Copy link
Contributor

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.

@citizenmatt
Copy link
Member Author

See also RIDER-15621

@citizenmatt citizenmatt modified the milestones: Rider 2018.2, Rider 2018.3 Aug 7, 2018
@diadas
Copy link

diadas commented Oct 24, 2018

See also #608

@citizenmatt
Copy link
Member Author

Closed with #1259

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants