Problems with large script

Hello, I was having a problem with unity crashing because it was running out of memory and traced it back to a script that has over 2000 lines of code in it. So I broke the script into several scripts to fix this. Not all of the lines are used at once, the way the script works is a function is called at the end of the start function as well as an integer it then uses if statements to find which block of code to use and then only needs about 100 - 200 lines of code to execute. My question is if anyone knows a max number of line you should use in one function? thanks

Javascript or C#?

I have used monolithic scripts at 4k lines and they work fine, I don't think there is a limit.

It shouldn't matter how many lines it is, lines don't really attribute to memory use, new objects do. If it's throwing memory exceptions, you're probably doing something pretty dodgy inside the code itself.

My question is if anyone knows a max number of line you should use in one function?

Well, this isn't a hard limit but more a practical limit for developers; I tend to avoid methods larger than 30 lines like the plague. Instead I often refactor code. This isn't because the program will run slow, but because I will code slowly if it was a huge "mess". I guess it's part subjective and part wisdom. It can become quite hard maintaining larger blocks of code. I guess everyone has their "pain thresholds" at some number of lines of code. Which is yours?

In fact, breaking into several functions cause a slight overhead but I don't try to squeeze out every single penny of my performance since I'd be working so much slower. I leave optimizations to things that matter. Reducing time/memory complexity, caching, and tightening out loops where I need them. Should I really need performance and I know it's because of these extra functions then I might opt to put them back inline again.

Readability is clearly improved if a part of the function says "UseJetpack()" instead of having several extra variables in the same scope, calling animations, decrementing fuel, playing sounds and so on.

It also be easier to cope with the relationship of variables if they are in clearly defined scopes. You know that a local variable in function X won't impact a local variable in function Y directly. This is also one of the reasons I limit my blocks of code to a handful.

Generally my functions are between 1 to 10 lines of code, but it's down to preference in my case. I usually create new functions if a method starts to grow out of hand. Refactoring in Visual Studio is so easy it becomes second nature.