C
C#2y ago
Pacrombie

❔ Singleton Pattern StackOverflowException

In the attached code, the GameManager is working fine, but InputManager is throwing a StackOverflowException at line 12. Any help or guidance is appreciated.
9 Replies
Pacrombie
PacrombieOP2y ago
(It won't even hit the Debug.LogError on line 14) I understand what's happening but I have no idea how to fix it.
Not Mark
Not Mark2y ago
Declare a private _instance field and have your Instance getter and setter get/set the field instead of getting and setting itself. Your Instance property checks if Instance (itself) is null, but that only calls the Instance getter which you are already in
Pacrombie
PacrombieOP2y ago
The private _instance should be static too, yeah?
Not Mark
Not Mark2y ago
Correct
Pacrombie
PacrombieOP2y ago
Works perfectly! Thank you so much :)
Not Mark
Not Mark2y ago
Happy to help! 🙂
ero
ero2y ago
The GameManager one can't be fine, that's the same issue
Pacrombie
PacrombieOP2y ago
The GameManager wasn't throwing an error because my game logic would hit the InputManager error and break before it could throw the GameManager error. So, yes, you're right, they were both broken.
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server