Stopping NoMethodError occurred. undefined method '[]' for nil:NilClass

So I am trying to use $game_switches[youFavoriteNumber] in my scripts.

What I get is: NoMethodError occurred. undefined method '[]' for nil:NilClass

Breaking the entire application/making in unplayable.

---

So does the script not know game switches at that point?

Should I start changing the "Main" script?

---

The issue seems to be about changing dynamic constants. That does not work despite their name...

So there are two ways:

(1) Set it before

(2) Change it during run-time

Constants can only do the first. For the second we need to use variables.

But the engine does not know the game_switches before so (1) does not work, changing a constant does not work later so (2) does not work either... 😔

Thanks for any form of help! 😢
It all depends on WHEN and WHERE you're using them. If they haven't been created, it will crash.
WHERE) Above MAIN like any materials script.

WHEN) Before MAIN is executed, that means the game is not running rn.
The location is still highly unspecific, we need to get many details here before we can tell you what's wrong.

The moment looks a lot like the same old story because it still depends on you providing us with all the details that will clarify what kind of mistake you're doing.

The only thing clear here is that the game switches have not been created at that point.
I am using a script (in materials) called Master Volume Control by Rutan/Ovate.

In module HZM_VXA -> module AudioVol something we call a constant is set:
TITLE_NAME = "Volume Control"

That makes no sense, it should be a variable... because, as expected, the constant does not want to be changed...............................................................

Constants should be used for pi, planck. Not names and descriptions.
WHERE) Above MAIN like any materials script.

WHEN) Before MAIN is executed, that means the game is not running rn.
the problem has nothing to do with main

the game variables (and switches) are created on new game or loaded on load/continue game.
They do NOT exist during the title screen, because the title screen is before the game is started.

so you can only access the game variables from those classes that run after the game is initialised and not outside those classes or in the main function.

I think you need to take three steps back and describe to us what you want to do, not how you want to achieve it. Only when we know what the effect in the game should be we can tell you how to get to that point.
the game variables (and switches) are created on new game or loaded on load/continue game.
They do NOT exist during the title screen, because the title screen is before the game is started.
This is false.
Look at the SceneManager.run (when the game first booted), there's DataManager.init to create everything.

-----
HOWEVER.
Looking at the evidence. The script runs above the main (WHERE) and before the main is executed (WHEN). If the script runs before the main (and by the run, it is not about class/function definition, but an actual function call, etc), the script would not know the game objects such as $game_switches and $game_variables. Since those are created when the main is executed.

If you want to get more help, post the full script (or link to the script if it was someone's else) and full information on how you did it.

Original thread: https://forums.rpgmakerweb.com/threads/stopping-nomethoderror-occurred-undefined-method-for-nil-nilclass.164840/