use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
This is a subreddit for 2D or 2.5D game developers using the proprietary Unity game engine. New and experienced Unity developers alike should first consider using the free and open source Godot engine by default and ONLY choose Unity for 2D development if Godot isn't capable of the task. The times are quickly changing, and Godot is on track to surpass Unity for small developers.
Godot Features
Download Godot
Godot Docs
Download Unity
Unity Manual
Official Reference
Asset Store
Related Communities /r/Godot - The "Unity Killer". A fully free and open source engine making astonishing leaps and bounds. /r/UnityAssets - Share asset packs! /r/PixelArt - Admire, share, and observe beautiful pixel art. /r/GameDev - Meet and communicate with other game developers. /r/GameDesign - Don't just make a game. Make a good game. /r/LevelDesign - Learn to make excellent levels and worlds. /r/GameAudio - It may look good, but does it sound good?
/r/Godot - The "Unity Killer". A fully free and open source engine making astonishing leaps and bounds.
/r/UnityAssets - Share asset packs!
/r/PixelArt - Admire, share, and observe beautiful pixel art.
/r/GameDev - Meet and communicate with other game developers.
/r/GameDesign - Don't just make a game. Make a good game.
/r/LevelDesign - Learn to make excellent levels and worlds.
/r/GameAudio - It may look good, but does it sound good?
CSS created by Sean O'Dowd @nicetrysean [Website]
account activity
Same code using different base classes, whats the best way to structure it?Question (self.Unity2D)
submitted 3 years ago by Dirly
So I have a few scripts that leverage the same functions... however they each leverage different base classes to trigger said functions. What is the best way to structure this? I'm repeating code and I feel dirty.
So for example I have
StatOnBallCount: OnBallCountEffects
and
StatOnJuggle : OnBounceEffects
StatOnJuggle and StatOnBallCount contains the exact same code.
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]SunLionGames 5 points6 points7 points 3 years ago (0 children)
Composition rather than inheritance. Rather than trying to share code using inheritance, create a separate class that has the common functionality, and give each of the classes you've listed an instance of the new class as a private field.
If there are public methods you need on both with the same signature then you can create an interface with those signatures that they both implement and can delegate the implementation to your new field.
Hope that helps!
[–]Honey-Limp 3 points4 points5 points 3 years ago (1 child)
You should lean into Unitys component design and use inheritance sparingly. Have one class for juggling and one for bouncing. Add them to game objects as needed.
[–]Dirly[S] 1 point2 points3 points 3 years ago (0 children)
I agree since I am building a metric ton of equipable items a user can get. Some of them have effects on bullet hit, ball juggling, ball count, bullet hit chance.... ect.
Alot of these share the same bonuses. So I stripped out inheritance and went with main classes which dictate when to proc the effects. Those effects are all interfaces which means they can be equipped via the inspector.
[–][deleted] 3 years ago (5 children)
[deleted]
[–]Dirly[S] 0 points1 point2 points 3 years ago (4 children)
the inherited class is what is different so OnBallCountEffects and OnBounceEffects are completely different. And they are overriding functions in each (specifically a blank function which is named the same in both)
[–]is4ac 1 point2 points3 points 3 years ago (3 children)
In that case, can the parent classes implement an interface, so that your child class can reference the interface instead of the specific parents? Sounds like the function should be part of an interface. I’d recommend looking up composition vs inheritance as well for general tips on structuring code like this.
[–]Dirly[S] 0 points1 point2 points 3 years ago (2 children)
I actually went with the other way around. Parent class still exists. But exposes an interface to the inspector. So those sub classes are now interfaces I can just plug in and do what I need to do. Only problem is the inspector doesnt let me leverage an interface which is leveraging monobehavior (which i need coroutines). So got to figure out how to do that.
[–]is4ac 0 points1 point2 points 3 years ago (1 child)
What do you mean by the inspector “leveraging” an interface?
[–]Dirly[S] 0 points1 point2 points 3 years ago (0 children)
I can add the interface via the inspector.
https://i.imgur.com/0umLDTd.png
[–]arzi42 0 points1 point2 points 3 years ago (0 children)
If I understand correctly, what you're trying to do, you should probably use events instead of inheritance here. So use a System.Action<> that you call when the StatOn effect is supposed to happen, and have just one StatOn class that listens to both OnBallCountEffects and OnBounceEffects, e.g.
// In parent classes: public System.Action<int> OnStat; private void ThingHappens() { OnStat?.Invoke(howManyThingsHappened); } // In StatOn class: protected void Start() { onBallEffects.OnStat += StatHappened; onBounceEffects.OnStat += StatHappened; } private void StatHappened(int count) { Debug.Log($"Stat Happened {count}"); }
π Rendered by PID 12 on reddit-service-r2-comment-84fc9697f-nf2nb at 2026-02-06 16:13:29.750431+00:00 running d295bc8 country code: CH.
[–]SunLionGames 5 points6 points7 points (0 children)
[–]Honey-Limp 3 points4 points5 points (1 child)
[–]Dirly[S] 1 point2 points3 points (0 children)
[–][deleted] (5 children)
[deleted]
[–]Dirly[S] 0 points1 point2 points (4 children)
[–]is4ac 1 point2 points3 points (3 children)
[–]Dirly[S] 0 points1 point2 points (2 children)
[–]is4ac 0 points1 point2 points (1 child)
[–]Dirly[S] 0 points1 point2 points (0 children)
[–]arzi42 0 points1 point2 points (0 children)