• RuntimesUnity
  • BoundingBoxFollower not working when added by script

Hi, basically I wrote a code that use a dictionary to create child gameobject to the character and then added specific weapon's slot and bones during the runtime. However the colliders are not working. Strangely it only works during the Play when I clicked the hitbox child gameobject and view it in the Inspector, once the inspector shows, the collider immediately works.

void CreateWeaponHitbox(string weaponDef)
{


GameObject weaponHitBox = new GameObject(weaponDef);
weaponHitBox.transform.parent = this.transform;


BoundingBoxFollower boundingBoxFollower = weaponHitBox.AddComponent<BoundingBoxFollower>();
boundingBoxFollower.isTrigger = true;
boundingBoxFollower.slotName = _WeaponDefs[weaponDef].HitBoxSlot;

 
BoneFollower boneFollower = weaponHitBox.AddComponent<BoneFollower>();
boneFollower.boneName = _WeaponDefs[weaponDef].HitBoxBone;

  
Rigidbody2D rb = weaponHitBox.AddComponent<Rigidbody2D>();


SpawnedChildFollower spawnedChildFollower = weaponHitBox.AddComponent<SpawnedChildFollower>();
SlashDamage slashDamage = weaponHitBox.AddComponent<SlashDamage>();
slashDamage.power = _WeaponDefs[weaponDef].Power;

  
boundingBoxFollower.Initialize(true); 
boneFollower.Initialize(); 

} The initialization won't help either

Related Discussions
...

It seems like the issue you're encountering is related to the initialization of the BoundingBoxFollower and BoneFollower components. The colliders only start working when you view the hitbox child GameObject in the Inspector, which suggests that the components might not be properly initialized or updated during runtime.

Here are a few suggestions to ensure proper initialization and updating of the components:

  1. Ensure Proper Initialization: Make sure that the BoundingBoxFollower and BoneFollower components are properly initialized after setting their properties.

  2. Force Update: You might need to force an update on the components to ensure they are properly set up. This can be done by calling LateUpdate on the BoundingBoxFollower and BoneFollower components.

  3. Order of Initialization: Ensure that the initialization of the components is done in the correct order and after all properties are set.

Here is an updated version of your CreateWeaponHitbox method with these considerations:

void CreateWeaponHitbox(string weaponDef)
{
    GameObject weaponHitBox = new GameObject(weaponDef);
    weaponHitBox.transform.parent = this.transform;

    BoundingBoxFollower boundingBoxFollower = weaponHitBox.AddComponent<BoundingBoxFollower>();
    boundingBoxFollower.isTrigger = true;
    boundingBoxFollower.slotName = _WeaponDefs[weaponDef].HitBoxSlot;

    BoneFollower boneFollower = weaponHitBox.AddComponent<BoneFollower>();
    boneFollower.boneName = _WeaponDefs[weaponDef].HitBoxBone;

    Rigidbody2D rb = weaponHitBox.AddComponent<Rigidbody2D>();

    SpawnedChildFollower spawnedChildFollower = weaponHitBox.AddComponent<SpawnedChildFollower>();
    SlashDamage slashDamage = weaponHitBox.AddComponent<SlashDamage>();
    slashDamage.power = _WeaponDefs[weaponDef].Power;

    // Initialize the components
    boundingBoxFollower.Initialize(true);
    boneFollower.Initialize();

    // Force update to ensure proper setup
    boundingBoxFollower.LateUpdate();
    boneFollower.LateUpdate();
}

Additionally, ensure that the BoundingBoxFollower and BoneFollower components are correctly set up in the Inspector and that the slotName and boneName properties are correctly assigned.

If the issue persists, you might want to check if there are any other dependencies or settings that need to be configured for the BoundingBoxFollower and BoneFollower components to work correctly during runtime.

No, it won't work. and boundingBoxFollower does not have LateUpdate(). the slot and bone are correct, hence it works after I clicked to view its inspector in editor. Can I get a real person reply here? I bought the pro edition

@bahamut8001 Your code above is missing assignment of the target skeletonRenderer at both BoundingBoxFollower and BoneFollower components.

SkeletonRenderer skeletonRenderer = this.GetComponent<SkeletonRenderer>();
BoundingBoxFollower boundingBoxFollower = followerObject.AddComponent<BoundingBoxFollower>();
boundingBoxFollower.skeletonRenderer = skeletonRenderer;
...
BoneFollower boneFollower = followerObject.AddComponent<BoneFollower>();
boneFollower.skeletonRenderer = skeletonRenderer;