I am having issues coding a game, in terms of variables in objects. Whgen running i get this error

action number 1
of  Step Event0
for object obj_text_demo_player:

Variable obj_gameManager.<unknown variable>(100020, -2147483648) not set before reading it.
at gml_Object_obj_text_demo_player_StepNormalEvent_1 (line 1) - if  (obj_gameManager.ammo == 0)

and i have no clue hope to fix this, this is where the code is used.

obj_player (player - where the issue seems to be - STEP EVENT)

if (obj_gameManager.ammo == 0)
     obj_gameManager.weapon = "MachineGun";
    obj_gameManager.ammo = 50000;

obj_pickup (pickup object - COLLISION /W PLAYER OBJECT)

obj_gameManager.weapon = "Tri-Shooter";
obj_gameManager.ammo = 900;

Obj_gameManager (object variables are from - DRAW EVENT)

global.health = 100;
global.weapon = "MachineGun";
global.ammo = 50000;


draw_text(780, 300, global.weapon );
draw_text(780, 375, global.ammo );

Any help would be massively appreciated, as I'm new to Game Maker Studio. If you need any more info I'm happy to add it.


It seems to me that you didn't declare the variables in the Create event, since it says <unknown variable>. In the Create event of obj_gamemanager you need to setup like

this.ammo = 0;

And do that every single variable that you have, otherwise they won't exist.

  • \$\begingroup\$ I have tried this, both adding the global.ammo as well as trying "this.ammo" neither work, with "this.ammo" giving a different error entirely. \$\endgroup\$ – K.Denn Apr 6 '16 at 20:47
  • \$\begingroup\$ @K.Denn But did you tried to initialize the variable in the Create event? Without "using" the variable in the Create event, the variable won't exist \$\endgroup\$ – DH. Apr 6 '16 at 21:27
  • \$\begingroup\$ global.weapon = "MachineGun"; global.ammo = 50000; This is how i did it, is this the correct way? i also have tried the same thing but with "global" changed to "this" with the same result. If you would prefer, i can give you the GMK so you can look under the hood better. \$\endgroup\$ – K.Denn Apr 6 '16 at 21:32
  • \$\begingroup\$ @K.Denn Well... you can try to use global for now, but, everytime you try to read the variable it's global.variable, and not obj_gamemanager.variable. There's something troubling me though... the object with a problem is obj_text_demo_player when trying to access the obj_gamemanager, it looks like that it's trying to read obj_gamemanager as a variable and not as an actual instance. Can't help you now though, I don't have GMS here, but if IIRC, the color of obj_gamemanager should be different, I think it's golden color. \$\endgroup\$ – DH. Apr 6 '16 at 21:46
  • \$\begingroup\$ After making the adjustments the code now works, thanks. \$\endgroup\$ – K.Denn Apr 6 '16 at 21:54

In global game settings click "set uninitialized variables to 0".

This isn't recommended all the time in programming practices to assume undefined variables have initialized values, but if that is what you wish to do then so be it.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.