Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am making a small puzzle based game. I want the number of instances of an object to be equal to the ammo count plus 2.

Each time a new room is loaded, the main object's creation method with run this script:

var ammo = instance_count(jellyfish)+2;

Then each time a bullet is fired, I subtract one. When there is no ammo left, the player goes to the 'failure' room.

However, I get the error: Unknown function or script: instance_count.

I have see this script on Google, but am not sure if it has deprecated or not. Is there a way I can do this?

share|improve this question
up vote 3 down vote accepted

The function you want is instance_number(obj) (documentation found here).

This functions returns the number of instances of a object that exist in the current room.

share|improve this answer

if you used instance_count you could do

share|improve this answer
Can you edit your answer to include the documentation surrounding the instance_count property? I don't see it. – Byte56 Feb 20 '15 at 23:34
instance_count returns the all the active instances in the room: instance_count – xpy Mar 15 '15 at 10:46

use instance_number(yellyfish) instead of instance_count. Instance_count are ALL objects and instance_number(obj) is the count of A SPECIFIED object.

share|improve this answer
Welcome to the site. Because this question was asked so long ago, your answer appeared in the review queue of late answers. Unfortunately, your answer repeats the answer that was accepted long ago, so there it does not add helpful information. If you want to reiterate that this solution helped you, vote up the accepted answer. Multiple identical answers don't improve the quality of information of the site. – Seth Battin Aug 23 '15 at 17:47

I think it is because there is no jellyfish instances in the failure room. What you can do (I don't think this works in gamemaker 8.1, you actually have to use the room number, which is an integer (1, 2, 3, etc.)), but assuming you are using gamemaker studio you should use this, where failureroom is the name of the failure room:

if room != failureroom
    {var ammo = instance_count(jellyfish)+2}

In this script the curly brackets are not necessary, but if you have more lines of code in the test like maybe you set the health as well, you have to use it.


if room != failureroom
    {var ammo = instance_count(jellyfish)+2
    var health = instance_count(jellyfish)+2}

If you do:

if room != failureroom
    var ammo = instance_count(jellyfish)+2
    var health = instance_count(jellyfish)+2

It will always change the health, even if you're in the failure room, but it will only change the ammo if youre not in the failure room.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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