Hello,
I have animation events in an attack animation that send a flag at the beginning of the animation to enable the mesh collider on a weapon that I’m using to detect a collision with an enemy, then it sends a flag to disable it at the end of the animation. Here is my code for that script:
public class WeaponHook : MonoBehaviour
{
public DamageCollider[] damageColliders;
public void Init()
{
damageColliders = transform.GetComponentsInChildren<DamageCollider>();
for (int i = 0; i < damageColliders.Length; i++)
{
damageColliders.coll.isTrigger = true;
damageColliders.coll.enabled = false;
Debug.Log(damageColliders.Length);
}
}
public void SetColliderStatus(bool status)
{
bool bStatus = status;
for (int i = 0; i < damageColliders.Length; i++)
{
var collider = damageColliders.coll;
collider.enabled = bStatus;
Debug.Log("colliders active = " + damageColliders.coll.enabled);
}
}
}
WeaponHook is attached to the instantiated prefab, which is a parent object which holds the actual weapon model as a child. DamageCollider is a script attached the weapon model. The prefab is instantiated then parented under the RightHand of my model character. Here is the code for the DamageCollider Script:
public class DamageCollider : MonoBehaviour
{
public Collider coll;
public CharacterStateManager ourStates;
private void Start()
{
ourStates = GetComponentInParent<CharacterStateManager>();
gameObject.layer = 9;
if (coll == null)
coll = GetComponent<Collider>();
}
private void OnTriggerEnter(Collider other)
{
CharacterStateManager states = other.transform.GetComponentInParent<CharacterStateManager>();
if (states != null)
{
if (states != ourStates)
{
states.OnHit(ourStates);
}
}
}
}
The weapon hook script correctly shows in the inspector that DamageCollider script is indeed stored as a reference from this code on start. The console also shows the debug log statement correctly stating true then false at the specified points in the animation. The issue I’m having is that even though “damageColliders.coll.enabled” is indeed showing true then false, it never actually enables /disables the collider, so no hit detection occurs.
The most interesting part of this is that I have the collider enabled by default in the prefab. The first time I pick the weapon up in the scene and equip it, it is active, but stays active and I can damage the enemy indefinitely. If I unequip it then re-equip it, it is disabled and stays that way. When I go back and inspect the prefab from the project window, it has also been disabled and I have to disable it manually again before hitting play again. Any thoughts on this would be greatly appreciated.
Thank you,
