Refactoring Monolithic MonoBehaviours (Unity Tutorial)

Sign up for the Level 2 Game Dev Newsletter: eepurl.com/gGb8eP
In this Unity tutorial, I'm gonna show you how to refactor Monolithic MonoBehaviours so you never have to deal with them again.
#Unity3D #UnityTutorial #GameDevelopment
A Monolithic MonoBehaviour is a class that has too many responsibilities and hundreds of lines of code. They're hard to work with and can be the bane of your Unity project.
📦 Download the project code at / 34451321
🎁 Bonus Code - / bonus-monolithic-34519658
👨💻 Join our community: / discord
❤️ Support the channel: / infalliblecode
My Favorite Unity Assets 💯⤵️
1️⃣ Odin Inspector: assetstore.unity.com/packages...
2️⃣ Shapes: assetstore.unity.com/packages...
3️⃣ Easy Save: assetstore.unity.com/packages...
4️⃣ Dialogue System for Unity: assetstore.unity.com/packages...
5️⃣ Editor Console Pro: assetstore.unity.com/packages...
⚡ Learn more about Unity 3D Plus at prf.hn/click/camref:1100l3e8M/...
👋 Contact me directly at charles@infalliblecode.com
* Disclosure: These are affiliate links, which means I'll receive a commission if you use them to make a purchase.

Пікірлер: 76

  • @171694
    @1716944 жыл бұрын

    I wish I had a friend like Charles to help with my codes...

  • @InfallibleCode

    @InfallibleCode

    4 жыл бұрын

    I’ll be your friend but I can’t promise that I’ll randomly show up at your office and help you write code xD

  • @ShiNijuuAKL

    @ShiNijuuAKL

    4 жыл бұрын

    @@InfallibleCode If you do please remember to copy my appearance

  • @UmarFarooq-cu3gw

    @UmarFarooq-cu3gw

    3 жыл бұрын

    Appreciated!

  • @KDEDflyr55
    @KDEDflyr553 жыл бұрын

    Every time I watch one of these, I always think “this is weird.” Then 10 minutes later “holy shit that was effective.”

  • @InfallibleCode

    @InfallibleCode

    3 жыл бұрын

    Hah! A lot of folks have the same experience. Just let it happen 😂

  • @okito
    @okito4 жыл бұрын

    imagine a class so monolithic you need VR headset to look at it =)

  • @InfallibleCode

    @InfallibleCode

    4 жыл бұрын

    👀

  • @BeeGameDev
    @BeeGameDev4 жыл бұрын

    Dude I have so much respect for all the effort you put into these tutorials. The duel narrative adds a tone of perspective. I've got 3+ years in unity & c# as a programmer & technical artist, but I mainly watch your tutorials to help me be a better communicator. Awesome work!

  • @jar5472
    @jar54724 жыл бұрын

    Never wrote here. But watched alot of your videos. I think this format is a really good way to teach people how to seperate their scripts. Would like to see more this way. For example some regular c# classes instead of monos with scriptable objects as settings. Could be a nice extentions.

  • @InfallibleCode

    @InfallibleCode

    4 жыл бұрын

    Thanks for your suggestions! There’s a lot of stuff I’d like to cover and those are at the top of my list.

  • @playtech5362
    @playtech53624 жыл бұрын

    You`re a good Teacher , Thanks For This

  • @sugarsores6521
    @sugarsores65214 жыл бұрын

    First time watchin' you guy(s)! Good stuff, very excited for part 2!

  • @dreamisover9813
    @dreamisover98134 жыл бұрын

    Great topic to touch on! Helping with understanding of the general mindset can go a long way.

  • @ewwitsantonio
    @ewwitsantonio4 жыл бұрын

    Great video. I actually had the same exact conversation a couple weeks ago at a coding lab meetup. I brought in my unity project for some help. Looking forward to the next video! The reason I usually get so stuck is I don't know good design patterns to get the different classes to interact with one another. I rely heavily on properties, and calling functions inside the 'set' sections. It works in small projects but I run into issues quickly.

  • @TheNamesJT
    @TheNamesJT4 жыл бұрын

    8:50 where is the video mentioned at that timestamp?

  • @Maquiz89
    @Maquiz893 жыл бұрын

    This video is exactly what I needed very well done!

  • @unityhelp
    @unityhelp4 жыл бұрын

    This two guys are the best!

  • @Krblshna
    @Krblshna3 жыл бұрын

    wow, that's was great video man

  • @kodd10
    @kodd102 жыл бұрын

    Great video!

  • @hamzaakhtar6759
    @hamzaakhtar67592 жыл бұрын

    amazing tutorials

  • @Tantandev
    @Tantandev4 жыл бұрын

    "I was so excited to get this thing working, so I crammed all of the code into one single class" *Usually happens when I need to write a rather more complex system...* but I would rather refactor my code by figuring out what works/doesn't. Then to do *analyze paralize* Great video! The tools you used to refactor looks really cool, that speed!

  • @nyscersul42

    @nyscersul42

    4 жыл бұрын

    Analyse, paralise? what do you mean by that?

  • @Tantandev

    @Tantandev

    4 жыл бұрын

    @@nyscersul42 It basically means, that instead of starting to work on something, you are basically "paralyzed" by analyzing the task. Basically thinking about the problem more than actually working on it.

  • @nyscersul42

    @nyscersul42

    4 жыл бұрын

    Ah, i see... Im a proactive coder, i tend to analyze during construction :D

  • @jovannovakovic5975
    @jovannovakovic59754 жыл бұрын

    Awesome!

  • @InfallibleCode
    @InfallibleCode4 жыл бұрын

    📦 Download the project code at www.patreon.com/posts/34451321 🎁 Bonus Code - www.patreon.com/posts/bonus-monolithic-34519658

  • @stas_etc
    @stas_etc4 жыл бұрын

    Best Unity coding channel So professional

  • @PRodi_
    @PRodi_4 жыл бұрын

    Nice video! btw. if possible try to make more VR Quest related topics ;)

  • @salvador759
    @salvador7592 жыл бұрын

    You're so nice to yourself :-)

  • @lD3STlNY
    @lD3STlNY4 жыл бұрын

    Hi Charles! Am learning Unity since ~2 year now, and recently started to develop my first 3d game, an FPS. I created a Rigidbody based FPSController, it can walk, jump, wallRun, ledgeClimb, wallClimb, (tried step climb)... It's mostly polished and I think it feels great, but with everything it comes around 1400 (-250 debug and gizmo ) row of code, and I really don't know how to separate the script more then this. The script has nothing to do with camera or input handling, the reusable code is already reused, the only thing I could think of, is cut out the logic of wallrun and jump groundcheck etc. to different classes, but that seems to be a lot of work, and am really not sure, if it would improve readability at all. For the record, jumping consists of ~100 row, wallrun ~300, wallclimb ~200, ledgeClimb ~100, transition between movement states ~100... (Maybe have a bit more empty rows then common, and have some comments throwed in as well...) Would you separate the logic out in classes like WallRunHelper, JumpHelper, WallClimbHelper...? Leave it alone? Or something different? :D

  • @subliminalcastillo2126
    @subliminalcastillo21263 жыл бұрын

    Ride is available for free if you use their early access version! > www.jetbrains.com/rider/nextversion/ I found this out last night after quitting rider for a month due to financial reasons. This has changed the game for me, as I love rider to death, but just can't afford it right now. Spreading the message around on youtube.

  • @mariuszskowronski
    @mariuszskowronski2 жыл бұрын

    Hi, what if one of: gun, inputHandler, ammoText (or all of them) will be null ? Should we always check everywhere: if (ammoText != null) { /* do something */ } if (gun != null) { /* do smoething */} ... If we add this everywhere (in bigger class than in that example) then the code becames messy again? Or maybe we should assume that these scripts will definitely be set in the inspector and it cannot be otherwise?

  • @ZloyDinozavrik
    @ZloyDinozavrik4 жыл бұрын

    Thanks! I love your videos! I'm your huge fan! However, I would like to see fewer scenes with a polite cheap chat. This video is about coding after all.

  • @monkeyrobotsinc.9875

    @monkeyrobotsinc.9875

    3 жыл бұрын

    You're huge?

  • @Diablokiller999
    @Diablokiller9993 жыл бұрын

    What's the follow up video to this?

  • @openroomxyz
    @openroomxyz4 жыл бұрын

    Can i develop desktop (Steam) VR games / App in Unity with Oculus Quest with Link or I need rift?

  • @Flameo326
    @Flameo3264 жыл бұрын

    What editor are you using? Does it require any plug-ins to get the icons for assets used, Usages, and name above the class declaration?

  • @bob450v4

    @bob450v4

    Жыл бұрын

    rider it is paid. It has a lot of refactoring tools witch is nice

  • @darryljf7215
    @darryljf72153 жыл бұрын

    Behold my 2,000 line Team.cs monolith.

  • @tarutso
    @tarutso3 жыл бұрын

    Sorry, newbie here, does anyone know within Unity what you would need to do with the 'private Gun gun' field? Do you need to drag the Gun.cs onto GameController and then drag the script into the open field in the Inspector??

  • @TheKr0ckeR
    @TheKr0ckeR Жыл бұрын

    Good example, But arent' we just now increasing the coupling?

  • @apo8895
    @apo88954 жыл бұрын

    I know the shortcut to making a method is "Ctrl + ." , but what is the shortcut for the dialog box that just appeared for making methods?

  • @InfallibleCode

    @InfallibleCode

    4 жыл бұрын

    It depends on your keyboard layout but mine is Ctrl Alt M

  • @nyscersul42
    @nyscersul424 жыл бұрын

    Personally i think there are times when such a monolith is appropriate, sometimes. For example my neural network... splitting it is just likely to make it hard to use hehe but.. its a self regulating data object.

  • @Rhino123freak
    @Rhino123freak4 жыл бұрын

    How does this work on the unity editor side of hings? All these scripts will sit on the gameobject? you should've touched upon how exactly it'll work.

  • @klarnorbert

    @klarnorbert

    4 жыл бұрын

    Same as before, just more classes.

  • @kraemer-raimund

    @kraemer-raimund

    4 жыл бұрын

    AmmoLabel would be on the UI text object, Gun on the gun model etc.

  • @ridlr9299
    @ridlr92992 жыл бұрын

    My Enemy class (which derives from an abstract character class) is 600 lines of code and I have no idea where to even BEGIN refactoring. It does so many things, but they are all connected so tightly I don't know how to separate them.

  • @klarnorbert

    @klarnorbert

    2 жыл бұрын

    This video is just explained it.

  • @noodle-eater
    @noodle-eater4 жыл бұрын

    Sometimes, I also get lost when coding my game. e.g yesterday I create a quiz game, I split my code based on its responsibility but turn out that it still a mess. How I should create the boundaries for each class?

  • @sotsch9280

    @sotsch9280

    Жыл бұрын

    one popular option is create "horizontal" layers of functionality and "vertical" slices of context (like gamecontext -> what you like to accmplish ;-) ) . Also take a close look into SOLID principles, CRP, CCP and REP.

  • @freeofgamenotingness9288
    @freeofgamenotingness92884 жыл бұрын

    Why you use JetBrains Rider Insted Of Visual Studio

  • @Allan1291

    @Allan1291

    4 жыл бұрын

    I was wondering the same thing. Is there an advantage using it?

  • @SpiritedSeeker

    @SpiritedSeeker

    4 жыл бұрын

    Its basically visual studio with resharper and more fancy unity integration. It provides a lot of refactoring options and some other sexy features that are part of Intellij. Check out kzread.info/dash/bejne/aYCZus9qo8fXls4.html if you want more detail.

  • @freeofgamenotingness9288

    @freeofgamenotingness9288

    4 жыл бұрын

    @@Allan1291 I used Rider But It used 1GB of RAM So I prefer Visual Studio

  • @Allan1291

    @Allan1291

    4 жыл бұрын

    @@SpiritedSeeker Thanks a lot! I'll check it out and see if it can improve my workflow!!!

  • @Allan1291

    @Allan1291

    4 жыл бұрын

    @@freeofgamenotingness9288 I 'll keep that in mind, and check if it worth the extra memory cost.

  • @TairaGames
    @TairaGames4 жыл бұрын

    damn, I've been cut-pasting like a chump

  • @InfallibleCode

    @InfallibleCode

    4 жыл бұрын

    Gotta learn those shortcuts!

  • @RiorXD
    @RiorXD3 жыл бұрын

    In all the years of coding ive never thought to have the method be the if statement condition. :/

  • @monkeyrobotsinc.9875
    @monkeyrobotsinc.98753 жыл бұрын

    I legit like the bad acting.

  • @AWalrus
    @AWalrus4 жыл бұрын

    When do you plan on starting the acting tutorials

  • @monkeyrobotsinc.9875

    @monkeyrobotsinc.9875

    3 жыл бұрын

    He needs to watch some first

  • @nyscersul42
    @nyscersul424 жыл бұрын

    Whats all the automatic comments - says your name a lot - before every class i think

  • @RichyAsecas
    @RichyAsecas4 жыл бұрын

    Noobs of code smells! I'm facing with 9000+ lines classes in my current job. 😭

  • @HectorVizyon
    @HectorVizyon4 жыл бұрын

    50 line of class is a monolithic? That's a function in my company. We have 3500 lines of classes with 50 dependencies in them.

  • @artemisDev
    @artemisDev4 жыл бұрын

    I feel like Charles is being used by you, he helps you code and you make money off his talent. Do you even pay him? I'm pretty sure he's so good he won't take payment.

  • @ZaCkOX900
    @ZaCkOX9004 жыл бұрын

    This would make sense if shooting was something like 3,000 lines... but the lines are so short, so small for that 1 script... it makes no sense to break it down because now it is way more complex, it can be all over the place. You could add regions and organize that way. But too many scripts is worse. Imagine if you have 1,000,000 lines combined in a project... you going to manage 100,000 scripts? Good luck. Why is ammo a float? I wouldn't use awake when doing VR dev. I do have to shake your hand because you are doing a great job in making videos and you do know what you are doing. ++

  • @SteveTheExploiter

    @SteveTheExploiter

    4 жыл бұрын

    Because Boeing? But seriously, each one of those scripts has only 1 job. If you have an issue, you know which script is causing it. You are not searching or scrolling thru lines of code to find it. If you are managing a team and they are split up by namespace, you can also track who is responsible for what section just by looking at the namespace. When doing code testing, you can quickly add test points to single scripts that won't break anything else. When you have classes in separate files and you follow the no dependencies rule, if you fat finger some code somewhere your whole house does not come crashing down, just the single class your cat screwed up as she ran across your keyboard.

  • @ZaCkOX900

    @ZaCkOX900

    4 жыл бұрын

    Agreed, break it down when needed... not ALL the time though. I would accept the 1 mono script he had over the end result he made. In my opinion, you break it down based on complexity of what your programming. This project he displayed is not complex what so ever. And we could argue it's simple because it was made for a video to demonstrate what to do. Infallible made an awesome job showing that. Should you break things up like that? Certainly if you got so many lines it's hard to code. Managing a ton of people, teams, sure. But if he handed me that script, I would of instantly asked, why is this not 1 script at this time? If I take my UDP class, and the 3,000 lines it is, break it into many scripts, omg... that would be a nightmare. It's a class for what it does, UDP. That is a monster of complexity in itself.

  • @adribier
    @adribier2 жыл бұрын

    boundaries huh... yeah my ex-girlfriend was pretty good at setting boundaries for everything

  • @Jarzap
    @Jarzap4 жыл бұрын

    Are you guys twins??

  • @monkeyrobotsinc.9875

    @monkeyrobotsinc.9875

    3 жыл бұрын

    Ya

  • @sotsch9280
    @sotsch9280 Жыл бұрын

    sorry mate a class cannot be "monolothic"... the term "monolothic" refers to software architecture and the way components depend on each other and how they are deployed. When you design one class with all logic, then your architecture is monolithic due to the fact that everything is dreaded together as close as possible in one file (of course bad practice). This is the extreme case of "monolithic" architecture. Also people often confound "monolithic" with just "bad code". Sometimes, and also often with games, its better to keep things close so that you can deploy your game as single unit. so its makes senese to mainain a monolithic style. this does not mean that you bloat everything in just one class. Of course split things up. And Also if you split your Project into many smaller .dlls, building a dependency hierarchiy, its still "monolithic" because its build and deployed as one! The more you are able to replace single Components the more it leads you into something called "Modular monolith".If These components, which hopefully only depend in one single direction ("dependency rule"), are running on different environments, then we talk about services (like with "microservices"). please do not confuse "monolithic" with just bad coding practice... this is one of the big pitfalls when people try to "be smart and tell you how to do so".... also: Components are no classes!!!! -> classes CAN be a component, but the meaning of Component refers to deployment unit.. often a cluster of classes which are independently deployable :).