Agree. I also want AddComponent() with extra arguments to prevent component OnEnable() from running.
I am using Unity 2021. I am using Zenject, which allows giving MonoBehaviours default “static-like” variables, such as [Inject] private AttacksManager m_attacks; These [Inject] fields are assigned for objects that exist on scene start, or can be assigned-on-instantiate through a large number of spawner helper functions like newComponent = Zenject.DIContainer.InstantiatePrefabForComponent(), or newComponent = Zenject.DIContainer.AddComponent(), or (the most reliable one) DIContainer.InstantiatePrefabForComponent()
There is high risk of null errors if one forgets to initially “install” the AttacksManager through a Zenject.MonoInstaller (which is ordered to run before other scripts on scene play) or if one forgets to chain [Inject] installs through Zenject.DIContainer.InstantiatePrefabForComponent() and Zenject.DIContainer.InstantiateComponent()
So I spawn an attack, which adds a component to itself, and the new component’s [Inject] variable is null in OnEnable(). >:(
Strange! So I make sure to Debug.Log() them and yes, the Attack has [Inject]s as well as a [Inject] DIContainer so that it can create new runtime objects with [Inject]s
So, the attack references its valid [Inject] DIContainer to call container.InstantiateComponent(gameObject);
And component T gives a null error for an [Inject] variable in OnEnable() !! >:( >:(
That’s strange, Zenject should be assigning that before any Unity code runs…
What happens on stepping into var c = container.InstantiateComponent(gameObject)?
public Component InstantiateComponentExplicit(
Type componentType, GameObject gameObject, List<TypeValuePair> extraArgs)
{
Assert.That(componentType.DerivesFrom<Component>());
FlushBindings();
var monoBehaviour = gameObject.AddComponent(componentType);
InjectExplicit(monoBehaviour, extraArgs);
return monoBehaviour;
}
Obviously, Zenject adds the component.
var monoBehaviour = gameObject.AddComponent(componentType);
And then Component.OnEnable() is called IMMEDIATELY !! !! !! >:( >:( >:(
NULL REF ERROR on [Inject] components
And then Zenject injects components 
InjectExplicit(monoBehaviour, extraArgs);
Very frustrating.
A similar function DIContainer.InstantiateComponentOnNewGameObject has the same problem.
Luckily, InstantiatePrefabForComponent() has always assigned [Inject]s before running OnEnable, so I must stick to pre-created components/prefabs for anything that need [Inject] variables.
Please fix