Advertisement

How to plan and structure your code in Unity?

Started by December 06, 2017 09:09 PM
1 comment, last by trjh2k2 6 years, 9 months ago

Heyo,

I'm new to Unity and have been wondering a bit on how to best structure the scripting part. What are the good practices? Bad? Currently I am adding a script component for each script which are based on what the script does, just to keep them apart and focused on their function. Ex. Have everything to do with combat in one script, selection in another,  movement in one, etc. And then use getComponent on the object if i need them to communicate in some way. Is this ok? It seems kinda off, even though having the code keep their single responsibility usually is good..
Would it be better to use script component, and then add the other parts as regular classes in that script? Last night i found this example, is it a good way of working with unity?
I'm not asking for a enterprise solution as i am just starting out with unity(but i work as a developer in c# & more), as i don't want to spend too much time when i have barely something to improve, just something to use as a guide in the future.

Unity is not different than any other engine or project in terms of how scripts might be organized - do what makes sense to you - or if you're on a team, do whatever the team is comfortable with.

I typically try to only make an object a component/script/monobehavior if that class needs to expose something to be tweaked in the editor, or if that script needs to otherwise be tied to a gameobject.  I'm not a unity expert, so maybe someone else has better reasons for doing otherwise, but creating a gameobject for no other purpose than to host a script has unnecessary overhead.

IMO the key is to do things in a way that makes sense to you, as long as you've answered the question of "why".  If you've got a reasonable answer to why you've structured things a particular way, then it's probably good.

This topic is closed to new replies.

Advertisement