Incorrectly adding objects?

I have two scenes, playScene and AddFood Scene. At the beginning of the playScene an apple object is created. On the AddFood scene there are several foods to choose from and below each food is a button. If the user presses the button the food is supposed to be added to the playScene by updating a global variable holding the number of that type of food and comparing the number of particular object to the number of foods there is supposed to be. Each food’s button has its instance variable which I use to check. But no matter which add button I press only Apples are added. For example, if I press the Add button underneath the banana, an additional apple appears, not a banana in the play scene. I don’t know what is happening and is there a better way to write this?

in my opinion, when you create the object you should define the object variable wich will identify the instances. for example call it “type”. when you clone (pull the instances to the scene) you should change only the value this variable. so the variable name remains the same, only the value (it can be a number or text) change. for example: 0, 1, 2 or “apple”, “banana”, “broccoli”.
after that, you should check always the same variable’s value. so:

  • if variable:type of AddButton = “apple” (or “0” in your screenshot) :arrow_right: do this
  • if variable:type of AddButton = “banana” (or “1”) :arrow_right: do that

Not that it should change anything, but “a better way to write this” would be:


Or even better with variable structures, but it would be more complex. Maybe later? :slight_smile:

If you don’t understand what is happening, use the debugger to check the number of instances and the variables, or use your Console object to display any data you’re missing. Check everything, step by step.

1 Like

I changed my code. No more apples are being created when I press the add button under banana but no bananas are being created either. When I check the number of bananas there is 1 banana after I press the add button, but I can’t see it on the screen. I tried changing the z-order but it isn’t working. Thanks.

I tried to see if my conditions where working so changed the banana object to an apple so when I pressed the add button under banana, an apple was created. When I changed it back to a banana object to be created and I pressed the add button under banana, nothing was created.

from that I don’t see what’s wrong. it looks like your banana object is incorrect somehow. can you upload your project? I have a little time, I can look at it. but errr… its not a “how do I” question, it’s bugfixing. Gruk provided guidance for that one post above.

If the banana exists (according to the debugger) but you can’t see it, it’s a problem of layer or z-order.
Feel free to check out GDevelop Problem Solver by Gruk when you have a problem. :wink: