I am making a tower defence game, and I refered this example to code so that the enemies will be deleted when its hp variable is less than or equal to 0:
Hi, so just for understanding, the upper events are not yours, thatâs just an example you found?
The second event should just work, if the hp is lower or equal to 0.
Your spawn event should also work as intended. What are you doing with the TargetRoundButton after creation? If you donât move it anywhere the buttons will just spawn at the same spot and it may look as there is just one of them without disappearing. Maybe thatâs obvious but it is hard to say what is going on without seeing your other events.
Yes, the first image is an example while the second one is my code
I did couple of things after creating the TargetRoundButton. I let it move towards an object (with pathfinding behavior) and when the TargetRoundButton goes near enough the tower placeholder with animation#1 (indicating a tower is placed on the tower placeholder), the tower placeholder will create bullet, starting from tower placeholder towards the TargetRoundButton, subtracting 2 hp from TargetRoundButton. Hereâs the code related to everything mentioned above:
HmmmâŚThatâs strange⌠I tried the spawn event in another scene it works. I think I will try to copy everything in the new scene and try to run it again
After copying every single line of code and objects (with same name) into a new scene, I realise that the code does not spawn any TargetRoundButton. The one TargetROundButton I mentioned earlier was not spawned by the code but is originally in the scene (I placed it on the scene).
Just adding one more check to @Drona comment that you could make: look if the variable hp of TargetRoundButt object is greater than 0. But look at the object and not the instance thatâs in the scene.
I am not very sure how I can use the debugger to check my code, but when I start preview and debugger and there isnât any error. Also Iâve checked:
There isnât any background
The TargetRoundButton that I placed on the screen moves perfectly, so the problem shouldnât be on the End
When you click âstart preview and debuggerâ you can see the preview and behind the debugger window. In the inspectors-window is a button ârefreshâ. After clicking it will show all the objects in your scene and also the instances of your spawned TargetRoundButton - if they are being spawned.
I hope Rasterisko is okay when I answer this question. In your events you delete TargetRoundButton when its hp-variable is 0. If you, by chance, spawn the instances of TargetRoundButton with 0 hp they will be immediately deleted, so you have to make sure that your object has an hp value over 0 as an object variable.
I tried it, there is only 1 TargetRoundButton (again, it is the one I placed it in the scene)
I am not sure if I did this correctly, I change the hp variable of TargetRoundButton at the beginning of the scene, after that spawn 1 TargetRountButton every 2 seconds. I think this may be related to my problem because actually there is another problem which is about the second-last line of code:
The money keeps adding 50 about every 2 seconds, which is the âspawnâ timer value.
Okay, you can set the object variable of your TargetRoundButton in the scene editor. You click on the three dots next to your object and choose âEdit object variablesâ. You have to make sure that the name of your variable is exatly the one you use in your events (case sensitive).
If you set the object variable at the beginning of the scene it applies this value only to the object that is in the scene and not to those that are created later.