Jump to content
  • Advertisement
Sign in to follow this  
Pether

Unity How to plan and structure your code in Unity?

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement

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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!