Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved Add colliders at OnTriggerStay to array

Discussion in 'Scripting' started by Poooiu, Sep 27, 2020.

  1. Poooiu

    Poooiu

    Joined:
    Oct 4, 2015
    Posts:
    4
    Hello.
    First of all I hope thats proper place to post this thread. I was thinking about puttng it at "Physics"...

    I would like to add Colliders that are hit during OnTrigerStay to array, to check which ones were damaged already during current weapon swing, to avoid damaging one enemy multiple times.
    For ease I decieded to take thier names and store strings in array.
    Alternatively I can use list, but I reckon array would be more efficient, since it is attack function, so it will be used quite often. Anyway i tired both options, but with no success.
    Thats how relevant code look like, but only OnTrigerStay is important as far as I am concerned.

    Code (CSharp):
    1.  
    2. [SerializeField] protected float m_StartCollisionDelay;
    3. [SerializeField] protected float m_StopCollisionDelay;
    4.  
    5. [SerializeField] protected LayerMask m_Mask;
    6. protected Collider m_Collider;
    7. protected bool m_ActiveAttackTrigger;
    8. public string[] m_SwingedEnemies;
    9. public int i = 0;
    10.  
    11.  
    12. protected IEnumerator SwingIE()
    13.         {
    14.             yield return new WaitForSeconds(m_StartCollisionDelay);
    15.             m_ActiveAttackTrigger = true;
    16.             yield return new WaitForSeconds(m_StopCollisionDelay);
    17.             m_ActiveAttackTrigger = false;
    18.             i = 0;
    19.             m_SwingedEnemies = null;
    20.         }
    21.  
    22. private void OnTriggerStay(Collider other)
    23.         {
    24.             if (!m_ActiveAttackTrigger) { return; }
    25.  
    26.             if (m_SwingedEnemies.Contains(other.gameObject.name)){ return; }
    27.             else
    28.             {
    29.                 m_SwingedEnemies[i] = other.gameObject.name;
    30.                 Debug.Log(m_SwingedEnemies[i]);
    31.                 i += 1;
    32.             }
    33.  
    34.             if (other.gameObject == m_Character.gameObject) { return; }
    35.  
    36.             if (m_Character is EnemyCharacter)
    37.             {
    38.                 if (other.GetComponent<EnemyCharacter>() != null) return;
    39.             }
    40.          
    41.             var damageable = other.GetComponent<IDamageable>();
    42.             if (damageable == null) { return; }
    43.  
    44.             Damage(damageable);
    45.         }
    46.  
    My console errors after performing swing at enemy.



    Those errors are showing up at line 27 of provided code. My only guess is that there are too many colliders at one momen, but I can be wrong ofcourse.
    How can i solve this issue? Am I doing something wrong?
     
    Last edited: Sep 27, 2020
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
  3. Poooiu

    Poooiu

    Joined:
    Oct 4, 2015
    Posts:
    4
    Ok, thanks to you I found out, that my array is empty. I didn't knew, that I can't pupulate array at definition, like this
    Code (CSharp):
    1. public string[] m_SwingedEnemies = new string[100]
    Now I'm populating it at Awake function. I also changed.
    m_SwingedEnemies = null;
    with
    Array.Clear
    And eveything is working as it should.

    Thank you very much for showing me the way.
     
    Kurt-Dekker likes this.