- Home /
Why does AddComponent not exist in the current context?
I'm trying to add a script (MenuStateEmpty) to a dictionary within gGameMode within my script ModeManager, a local instance of class StateMachine01.
The problem line in question is found within Start() of ModeManager...
gGameMode.StateAdd ("menuStateEmpty", AddComponent (MenuStateEmpty));
In MonoDevelop, the word 'AddComponent' is in red and the following error shows up in Unity :
"Assets/Scripts/Managers/ModeManager.cs(66,55): error CS0103: The name `AddComponent' does not exist in the current context"
All scripts inherit from MonoBehavior, with the state scripts implementing IMenuState.
I'm at loss to identify why AddComponent shouldn't work here! The 'new' keyword won't work as MonoBehaviors can only be added using AddComponent - only it's not in the current context!
Happy to post more code if requested.
All the best,
--Rev
Answer by Berenger · Jun 03, 2014 at 09:51 AM
AddComponent is defined in GameObject, not Monobehaviour. Just do :
gGameMode.StateAdd ("menuStateEmpty", gameObject.AddComponent (MenuStateEmpty));
Answer by unity_RJs1M1mPBz0Pxg · Apr 29, 2021 at 10:26 PM
what in the hell does this line of code do Berenger ?,what in the hell does this line do