• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

destroying instance. Right code based on if all certain object collected.

A

Ancestralsoul

Guest
I used this code, it ran the game but it didn't destroy the gate upon all object9's collected.

if instance_number(object9) == 0
instance_destroy(object10) ;

I tried this.
Could anyone help?
 

samspade

Member
I used this code, it ran the game but it didn't destroy the gate upon all object9's collected.

if instance_number(object9) == 0
instance_destroy(object10) ;

I tried this.
Could anyone help?
Try indenting at least, and I would also use brackets.

Code:
if (instance_number(object9) == 0) {
    instance_destroy(object10) ;
}
If that code doesn't work then there is likely a different problem. For example, maybe there is still an instance of object9 or a child of object9 somewhere. I would run the debugger to find out. Or maybe this code itself is not being called (i.e. its in a script, event, or code block that isn't trigger or is in an object that doesn't have an instance in that room). You could again run the debugger to find out, or use a show_debug_message.

And as always, remember to post your code using [code] and [/code] tags.

so you are saying that if there are no active instances of object9 then object10 should be destroyed?

Instance_destroy() only destroys instances of an object not the object itself.

https://docs.yoyogames.com/source/dadiospice/002_reference/objects and instances/instances/instance functions/instance_destroy.html
In GMS 2 at least (and I thought they went back and did it for GMS 1) you can use instance_destroy this way. If you do it will function like:

Code:
with (object10) {
    instance_destroy();
}
 
A

Ancestralsoul

Guest
I'll be checking it out again.

I was thinking of a different way, having a variable requirement of 4. Then each object adding to the variable up to the total of 4.

How would I go about using variable code to do this in an additive way?
 
In either your player object or some kind of controller object, you'd put a variable. Then, in whatever block of code that handles destroying object9, have it add one to this variable (object_with_variable.variable += 1). Then in the step event of the object tracking this requirement, do the following:

Code:
if(variable >= 4)
  {
  if(instance_exists(object10))
    {
    instance_destroy(object10);
    }
  }
Also, if you're planning on repeating this on every level of your game, be sure to reset the variable to 0 at the end of each room or else the gate in the next room will automatically be destroyed too.
And as a side note, if you're naming your objects object9 and object10 in your actual project, I would suggest renaming them to something that's easier to identify at a glance (obj_gate and obj_key, for example). You'll save yourself a lot of confusion in the long run.
 

Joe Ellis

Member
There isn't any reason why that code won't work, so I'm wondering where you're running that code? the step event of any instance will work, and probably it should ideally be in the moment when you collect object9
 
Top