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:
Ensure Proper Initialization: Make sure that the BoundingBoxFollower
and BoneFollower
components are properly initialized after setting their properties.
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.
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.