- Home /
Too subjective and argumentative
which is best scripting method among these 3 ? please view the image
I'd put your '`prefab = GetComponent()`' in the Reset()
monobehavior function, rather than Start()
.
This way, when the component is first created, it will be initialized with the RigidBody on the same object (if present). However, the user can still change this reference, maually in the inspector, without having it overwritten on Start()
(like your bottom-left example).
In OnEnable()
, I would put some validation; basically confirm prefab != null
, and display an error in the console, if so. I might even disable the entire component so you don't get null reference exception when you access prefab.gravityScale
By the way... I don't think your question should have been closed. It seems like your asking about the advantages and disadvantages of these various methods, and put some effort into trying them out yourself. Next time, just be a bit more careful with how you phrase the question so it doesn't get marked as 'subjective'.
Answer by TreyH · Oct 08, 2019 at 03:52 PM
Comes down to preference, assigning the component in the inspector is probably the most "performant" but this isn't really something whose performance you care about for a 1-time thing. You can argue that the .GetComponent
called from inputs in Update would be excessive, but there was someone arguing awhile back that they considered it more readable.
These sorts of things aren't cut & dry -- it's usually just whatever you and your team can read / maintain most easily.