Search Unity

Collider not enabling through script

Discussion in 'Scripting' started by geraldp48, May 9, 2019.

  1. geraldp48

    geraldp48

    Joined:
    Jul 30, 2018
    Posts:
    24
    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:


    Code (CSharp):
    1.    public class WeaponHook : MonoBehaviour
    2.     {
    3.         public DamageCollider[] damageColliders;
    4.         public void Init()
    5.         {      
    6.             damageColliders = transform.GetComponentsInChildren<DamageCollider>();
    7.             for (int i = 0; i < damageColliders.Length; i++)
    8.             {
    9.                 damageColliders.coll.isTrigger = true;
    10.                 damageColliders.coll.enabled = false;
    11.                 Debug.Log(damageColliders.Length);
    12.             }
    13.         }
    14.         public void SetColliderStatus(bool status)
    15.         {
    16.             bool bStatus = status;
    17.             for (int i = 0; i < damageColliders.Length; i++)
    18.             {
    19.                 var collider = damageColliders.coll;
    20.                 collider.enabled = bStatus;
    21.                 Debug.Log("colliders active = " + damageColliders.coll.enabled);
    22.             }
    23.         }
    24.     }

    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:

    Code (CSharp):
    1.     public class DamageCollider : MonoBehaviour
    2.     {
    3.         public Collider coll;
    4.         public CharacterStateManager ourStates;
    5.         private void Start()
    6.         {
    7.             ourStates = GetComponentInParent<CharacterStateManager>();
    8.             gameObject.layer = 9;
    9.             if (coll == null)
    10.                 coll = GetComponent<Collider>();
    11.         }
    12.         private void OnTriggerEnter(Collider other)
    13.         {
    14.             CharacterStateManager states = other.transform.GetComponentInParent<CharacterStateManager>();
    15.             if (states != null)
    16.             {
    17.                 if (states != ourStates)
    18.                 {
    19.                     states.OnHit(ourStates);
    20.                 }
    21.             }
    22.         }    
    23.     }

    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,
     
    Last edited: May 10, 2019
  2. Mokzen

    Mokzen

    Joined:
    Oct 10, 2016
    Posts:
    102
    geraldp48 likes this.
  3. geraldp48

    geraldp48

    Joined:
    Jul 30, 2018
    Posts:
    24
  4. geraldp48

    geraldp48

    Joined:
    Jul 30, 2018
    Posts:
    24
    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...
     

    Attached Files:

  5. Mythran

    Mythran

    Joined:
    Feb 26, 2013
    Posts:
    85
    Have you ever found what the problem was?
    Thanks
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    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://forum.unity.com/threads/using-code-tags-properly.143875/
     
  7. geraldp48

    geraldp48

    Joined:
    Jul 30, 2018
    Posts:
    24
    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.