linuxgurugamer / ksp_toolbar

Common API for GUI toolbars in KSP
BSD 2-Clause "Simplified" License
13 stars 14 forks source link

Fix logspam (in debug Unity player) due to incorrect usage of Unity APIs #12

Closed mwerle closed 5 years ago

mwerle commented 5 years ago

Unity APIs are not allowed to be called in field initializers or class constructors. Initialize your class in Awake() or Start() instead.


Tested with several mods which use the Toolbar and it seems to be working fine in both the released Unity player shipped with KSP 1.5 and the debug Unity player. No more exceptions in the debug player.

mwerle commented 5 years ago

Example exception from "output_log.txt":

UnityException: get_dataPath is not allowed to be called from a MonoBehaviour constructor (or instance field initializer), call it in Awake or Start instead. Called from MonoBehaviour 'ToolbarManager' on game object 'ToolbarManager'.
See "Script Serialization" page in the Unity Manual for further details.
  at (wrapper managed-to-native) UnityEngine.Application:get_dataPath ()
  at KSPUtil.get_ApplicationRootPath () [0x00000] in <filename unknown>:0 
  at Toolbar.ToolbarManager..cctor () [0x00000] in /var/lib/jenkins/workspace/G-L/ksp_toolbar/Toolbar/Internal/Toolbar/ToolbarManager.cs:35 
Rethrow as TypeInitializationException: An exception was thrown by the type initializer for Toolbar.ToolbarManager
PiezPiedPy commented 5 years ago

PR #11 already addresses this issue

mwerle commented 5 years ago

Ah, ok, nice one. I'll close this one then although our approach to fix DontDestroyOnLoad() is slightly different. I guess @linuxgurugamer can choose which one he prefers.

These Unity errors are pretty endemic throughout the KSP modding community though :( Almost every mod I take a look at has these issues. It's amazing most of it more or less actually seems to work.

linuxgurugamer commented 5 years ago

I wonder if this used to work in earlier versions of Unity without the current issues? Anyway, thank you. I'm slowly finding other instances of this in various places. fixing as I go, anything you find is always appreciated