The argument, in a nutshell, is that one of us wants to use AddComponent and the other one wants to use a singleton pattern that, in true c# and .Net style, has a single public static reference to itself, allowing access to all public variables and methods.
Obviously, not knowing what AddComponent actually does at a compiler level makes it difficult to determine if there is the potential for a performance hit if we use AddComponent in a bunch of classes instead of just calling the public static accessor and getting the data from that.
So, the question…
What the heck does the compiler do with AddComponent? Is it simply doing the same thing a public static reference does? What potential pitfalls are there and what, if any, performance hits are potentially lurking?
If we were to do a static reference, what potential pitfalls are there and performance hits are possible?
AddComponent creates a new instance of the class, adds it to the game object, adds it to the management lists to use “Find” etc.
So compared to a singleton it will always lose the time war, just cause instantiation is by definition slower than an object access.
For your case I would go with a singleton. not needfully one independent of game objects but definitely a unique object per scene / game
Thank you so much for your insight! I looked and looked and could not find anything in the unity docs about AddComponent, other than “you should use it.”
As a programmer, the lack of real information unity provides is annoying but at least the comm picks up the slack ;0
Technically you can think of AddComponent as unitys way of creating instances of Component / MonoBehaviour because for neither you must create them through the ‘new’ keyword, as thats not supported (only GameObjects are ‘new-ed’)
I’m unsure if the fact on new is in the docs, but I’m pretty sure that somewhere along the docs or the 2D tutorial the relevant facts on what AddComponent is meant for and what it does are mentioned. But I agree that when you come from vanilla c# it can be a bit confusing at first, but once you grasp Unitys more or less pure Component - Pattern workflow it starts to make sense