- Home /
Script doesn't enable even when enabled = true
I am programming a Spawn Manager that I only want to enable when the game enters a certain state. I have used the logic below with all of my menus and it works fine.
public class SpawnManager : MonoBehaviour, IEventListener
{
void Start()
{
...
enabled = false;
}
void Update()
{
...
}
bool IEventListener.HandleEvent(IEvent evt)
{
string rText = evt.GetName()
if(rText == "ChangeStateEvent")
{
StateManager.eGameState eState = (StateManager.eGameState)evt.GetData();
if(eState == StateManager.eGameState.PRELAUNCH_GUI)
{
enabled = true;
}
else if(eState == StateManager.eGameState.SCORE_MENU)
{
enabled = false;
}
}
}
}
The script is disabled from the start and is only enabled when it receives the correct ChangeStateEvent. I have watched the event come in and change enabled to true, yet the script doesn't update. The only way I've been able to enable the script is by pausing the game and enabling it through the inspector.
Any help would be much appreciated. I'm stumped.
Have you watched the OnEnable / OnDisable callbacks? are they called when you set enable to true? $$anonymous$$aybe it get disabled again from somewhere else?
I guess you checked that your listener is called with some Debug.Logs?
Also keep in $$anonymous$$d that you can also deactivate the GameObject itself while the component is still "enabled". This would also prevent the script from running.
I haven't checked the callbacks but I've walked through it in debug and watched it handle the event and change enable to true. I'll try using the gameobject.
Your answer
![](https://koobas.hobune.stream/wayback/20220613074050im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
NullReferenceException help 0 Answers
I need help with a script(brauche hilfe mit einen Script) 0 Answers