So I started using Events in my code and added a OnEnable and OnDisable to have the class be removed or added to the Event. However, when I was running Profiler, I noticed that GUI.Repaint is taking up alot of my memory and I read that OnEnable and OnDisable were/are GUI functions. So I want to know if these are slow functions and if there are better ways to remove classes from unity events. Here’s an simple example of what I’m doing.
They certainly would not be the source of your slowdowns above.
Also, do not subscribe in Awake() if you are doing OnEnable/OnDisable. It’s better to avoid Awake for subscriptions and just use OnEnable/OnDisable()
The ONLY way to reliably tell what your slowdown is involves either instrumenting the code yourself, or else opening the Profiler window and profiling your game running on the actual target hardware (not just in the editor).
OnEnable/OnDisable has nothing to do with the GUI directly. It is a common feature of all Unity Behaviours. What you’re doing is fine except that you probably want to remove the _player_input.OnJump += _player_input_OnJump; from Awake(), because you are already running it in OnEnable(), and currently you will subscribe to the event twice.
Yep your profiler is showing that your game is running as smooth as butter, and the only thing that is taking up significant time (at least in that one frame) is the editor itself redrawing its own windows.
OHHHHH! Okay! I’m sorry, I’m very new to this and thought the random slowdowns were attributed to my code. It might just be Unity being Unity. Thanks @PraetorBlue ! Thanks @Kurt-Dekker !
Yeah the unity editor itself often has those lag spikes. It’s kind of annoying. Wherever you see “EditorLoop”, that means it’s the editor and not your code (usually). If you actually make a build of your game and run that, the editor is taken out of the picture and then you can freely blame any lag spikes you see on yourself