Collider not enabling through script

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,

Please use code tags to make your code more readable.

Take a look at this article: Using code tags properly .

1 Like

Better?

the Update code works, but the other doesn’t for setting the status of the damage collider. debug.log shows the correct status… true, then false…

Have you ever found what the problem was?
Thanks

He probably did and failed to post it here.

But that’s not an excuse for you to necro-post and assume your error is in any way related to this one.

Instead, set yourself up for success and start your own fresh post… IT’S FREE!

When you post, here is how to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

This is the bare minimum of information to report:

  • what you want
  • what you tried
  • what you expected to happen
  • what actually happened, especially any errors you see
  • links to documentation you used to cross-check your work (CRITICAL!!!)

If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: https://discussions.unity.com/t/481379

Yes but it was over three years ago. I published that project in November of 2019 and moved on to others, and I can’t recall exactly what was causing the issue.