Unknown variable (Game Maker)

This error happens when you try to set or retrieve the value of a variable that has not been initialized yet. You can avoid this error by setting the Treat Uninitialized Variables as 0 in Global Game Settings. Check your variable spellings as well, remember that they are case sensitive.

If it is a global variable, you can initialize it by giving it a value in your first room's creation code. If it is a local variable, initialize it in the create event of the object.

The error can also happen if you do your coding correctly. This appears to be when there is a high object_index in the game file, for example "object327680". When you normally create a new object it gets an object_index of (current highest object_index+1), but when you delete an object the highest object_index does not reduce by 1. This means even if you delete many objects and are left with only a few, the highest object_index can still have a high value like "object327678". At this point things that worked well don't work well again. A test was made in Game Maker 5.3a in 15 May 2014, Thursday. A new game was launched and its first object was created, whose object_index was object0. The object was put in a new room. Then in the Create event there was written "a=object0.x", and in the Draw event there was written "Draw the value of variable a at relative position (0,0)". It worked well: the value of "a" was drawn at object0's x-position in the room. But when the same thing was done in a game file that had high object_index values(like object327680), the error of "unknown variable x" appeared.

The problem has been known to happen when object_index becomes so high that it clashes with the values of instance ids. For example, a room can have an object with index of 200,000 and there is also an instance with id of 200,000. If you use a function like "instance_exists(obj)",where obj is either an object_index or instance id, you may get "false" even if the object is in the room, because the game assumes you are looking for an instance with an id of "obj". The problem has been fixed by making more accurate functions for checking objects; for example, functions that check only for id, or only for object_index, but not both. The solution is at this web address:

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution 2.5 License.