Objects added by python script won't work properly

Hello,
I made a script that generates a random map, so all the objects in the scene are created by the script when the game starts.
My problem is that when I try to interact with an object, only one object of x amount will work.
For example, in my .blend file, there are 210 generated dirt tiles. After I pick up the pickaxe, if I mouse over and press E on a dirt tile, the tile should be removed and the value should go up by 1 in the inventory.
But this only works on one object, if I find the one that works, the tile is removed and the value added to the inventory is the total amount of tiles in the game.
After one dirt tile is removed another one will work and so on. Same goes for the trees, it seems they have to be picked up from bottom to top.
The working dirt tile is always the one under the first tree that can be picked up.

I recorded a video I hope this can explain better what I mean:
https://youtu.be/dsdsJG59QZ0

I can’t attach .blend as a new user so I uploaded it to google drive.
.blend file

So what I need is, when I press E on any tile, the tile should be removed and its value on the inventory should go up by 1.
Can anyone help me with this please?
Thanks!

use a ray sensor in the player,

ray----------this script
Keypress–/

if Time > 0 ----------and-------------add -1 to Time

import bge
cont = bge.logic.getCurrentController()
own = cont.owner
ray = cont.sensors['RaySensorName']
key = cont.sensors['KeyPressName']

if ray.positive and key.positive and own['Time']==0:
    own['Time']=30
    own[ray.hitObject['inventoryType']]+=1
    ray.hitObject.endObject()

1 Like

The ray sensor did it! Thanks a lot!

1 Like