r/badcode • • Oct 16 '20

c# Me when the

Post image
1.0k Upvotes

47 comments sorted by

View all comments

Show parent comments

16

u/[deleted] Oct 17 '20

As in Unity itself is bad code? Or people who use Unity tend to write bad code?

48

u/stickcult Oct 17 '20

I mean Unity attracts a lot of people who want to make games but have very little programming experience, so their code generally isn't the best, but I can't judge them for that. Gotta start somewhere. But yeah, Unity itself is basically the definition of tech debt, it's pretty rough to work with sometimes.

16

u/Waterprop Oct 17 '20

Unity Asset Store is great example of this. There's a lot of great stuff there but also bad too, especially code side (there's good code too!)

Biggest thing I've seen that many people pay no attention to garbage collection at all. I get it's automatically collected but that automatic collection is NOT cheap and free.

Unity uses the Boehm–Demers–Weiser garbage collector, a stop-the-world garbage collector. Whenever Unity needs to perform garbage collection, it stops running your program code and only resumes normal execution when the garbage collector has finished all its work. This interruption can cause delays in the execution of your game that last anywhere from less than one millisecond to hundreds of milliseconds, depending on how much memory the garbage collector needs to process and on the platform the game is running on. For real-time applications like games, this can become quite a big issue, because you can’t sustain the consistent frame rate that smooth animation require when the garbage collector suspends a game’s execution. These interruptions are also known as GC spikes, because they show as spikes in the Profiler frame time graph. In the next sections you can learn more about how to write your code to avoid unnecessary garbage-collected memory allocations while running the game, so the garbage collector has less work to do.

Source

9

u/Atrufulgium Oct 17 '20

Luckily there's the bandaid of smearing out the GC over multiple frames. This way people can ignore shitty code even longer! Sigh.

0

u/[deleted] Oct 17 '20

[deleted]

12

u/flapjackrl Oct 17 '20 edited Oct 24 '20

yeah it honestly just depends what you’re making though. there are fantastic unity games like night in the woods and hollow knight that take advantage of what the engine offers but obviously that is limited to one niche style. you can grow an apple tree with a good seed but the same won’t apply when you’re looking to grow an orchard of apricots.

0

u/[deleted] Oct 23 '20

[deleted]

2

u/PixxlMan Oct 27 '20

It's probably just that none of these advertise themselves as unity, and as such you don't associate unity with them

7

u/xezrunner Oct 17 '20

I went into Unity knowing that it's not going to be a good time if my code is all hacks.

I'm learning a lot about performance optimization and good practices along the way. So far, everything's been going amazing!

I have a friend that tried to get into Unity (2D), but they very quickly dropped it. They expected things to just work.. Unity is a full-fledged game engine, you'll need some introduction and coding background for it to be a pleasant experience.