Can't get collision from added object (scene.addObject)

Skip to the end for the basic jist.

I want to add a hitbox to an attack with scene.addObject(hitbox, pos). My plan is to get the collision list of the hitbox, check if “target” is there, do damage to target, then add target to a hitList so i can make sure not to inflict more damage to it if it’s in the list.

So I have my hitbox on layer 2, it has a collision sensor connected to a python script that just defines the sensor (it just feels like it’s more activated this way)

When I add the hitbox I WANT to do:


#Define the hitbox
hitbox = scene.addObject("hitbox", attack.position, 1)

#Get its collision sensor
col = hitbox.sensors["Collision"]


if col.positive:
    
    #Check for the player then do damage and add to hitList to "deactivate" it from further damage 
    if "player" in col.hitObject:
        target = col.hitObject
        _doDamageAndTheRestOfTheFunctions(target)
        

It recognizes hitbox.sensors as “Collision”, but when I check for collision on said sensor it wont respond with anything! Always None. I can see the hitbox hitting the target, but it wont find a hitObject. I assume it has something to do with the sensors of the original object not connecting with the added object…

I can only get a hit response by doing this:


scene.addObject("hitbox", attack.position, 1)
col = scene.objects["hitbox"].sensors["Collision"]

#Now I have to reference the hitbox through the scene

And now I can’t have more than 1 hitbox active at a time, if I do, like if multiple enemies spawn their atkboxes agains the player, the damage output is multiplied by how many enemies are attacking…

Basically: I can’t seem to access the collision from instanced objects added via scene.addObject, even though the added object is dynamic and has a collision sensor connected to a script.

Hope you understand and can help, thanks <3

Have you tried to access the hitObject by name? I could be wrong, but maybe if you tried…

if col.hitObject.name == “player”:

I usually use this method, for interactions with objects added from other scenes.

This isbone of the proplems of instanced objects… My 2 cents would be in stead of have the script run on every enemy I would have it on the player to detect the enemies and then send that damage to all object with property name “enemy”. That way you can have multiple objects hiting the sword and only applying a single damage to all of them

Think about the timing. You just added the object. You are still in the “controller” phase of that frame. The added object will start sensing within the next frame.