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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Help! Colliders in Arrays.

Discussion in 'Scripting' started by DanSingh, Dec 5, 2015.

  1. DanSingh

    DanSingh

    Joined:
    Feb 5, 2015
    Posts:
    98
    What I am trying to achieve is when the "Zombie" tagged objectS enter a certain trigger collider that the objects enters will be stored in an array. Put it only seems to execute only on one zombie or at least store one object into the array...I think, however, the player does seem to execute everything well. Please help.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class BombScript : MonoBehaviour
    5. {
    6.     //bool inRadius = false;
    7.     GameObject player;
    8.     PlayerHealth playerHealth;
    9.     GameObject [] zombie = new GameObject[50];
    10.     bool [] zombieInRadius = new bool[50];
    11.     bool inRadius;
    12.     int temp = 0;
    13.  
    14.     void Start()
    15.     {
    16.         player = GameObject.FindGameObjectWithTag ("Player");
    17.         playerHealth = player.GetComponent<PlayerHealth>();
    18.     }
    19.  
    20.     void StartDetonation()
    21.     {
    22.         if (inRadius)
    23.         {
    24.             playerHealth.TakeDamage (200);
    25.             inRadius = false;
    26.         }
    27.  
    28.         for(int i = 0; i<50;i++)
    29.         {
    30.             if(zombieInRadius[i])
    31.             {
    32.                 zombie[i].GetComponent<EnemyHealth>().TakeDamage(200,zombie[i].transform.position);
    33.                 zombieInRadius[i] = false;
    34.             }
    35.         }
    36.     }
    37.  
    38.     IEnumerator OnTriggerEnter(Collider other)
    39.     {
    40.         if (other.gameObject == GameObject.FindGameObjectWithTag ("Player") || other.gameObject == GameObject.FindGameObjectWithTag ("Zombie"))
    41.         {
    42.             if (other.gameObject == GameObject.FindGameObjectWithTag ("Zombie"))
    43.             {
    44.                 zombie [temp] = other.gameObject;
    45.                 zombieInRadius [temp] = true;
    46.                 temp++;
    47.             }
    48.  
    49.             if (other.gameObject == player)
    50.             {
    51.                 inRadius = true;
    52.             }
    53.  
    54.             yield return new WaitForSeconds(5);
    55.             StartDetonation();
    56.         }
    57.     }
    58.  
    59.     void OnTriggerExit(Collider other)
    60.     {
    61.         if (other.gameObject == player) {
    62.             inRadius = false;
    63.         }
    64.  
    65.         for (int i = 0; i<50; i++) {
    66.             if (other.gameObject == zombie [i]) {
    67.                 zombieInRadius [i] = false;
    68.             }
    69.         }
    70.     }
    71.  
    72. }
    73.  
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    GameObject.FindGameObjectWithTag

    this will return the first object it finds... so it'll be checking if the colliding object is a specific zombie

    if you just want to check "am i colliding with any zombie" just check the tag.
     
    Deleted User likes this.
  3. DMSolace

    DMSolace

    Joined:
    Nov 30, 2015
    Posts:
    9
    You can use an array of colliders to detect all collisions, so it will detect every individial zombie in your array of zombies.

    Code (CSharp):
    1.     IEnumerator OnTriggerEnter(Collider[] others)
    2.     {
    3.         //for loop repeats for everything that collides with trigger area
    4.         for(int i = 0; i < others.Length; i++)
    5.         {
    6.  
    7.             if(others[i].gameObject.tag == "Zombie")
    8.             {
    9.  
    10.             }
    11.             else if(others[i].gameObject.tag == "Player")
    12.             {
    13.  
    14.             }
    15.         }
    16.  
    17.         yield return new WaitForSeconds(5);
    18.     }
    Edit: You will need to add an if statement to prevent null reference exception. Before the if loops add if(others != null). then add an else case where it returns.
     
    Last edited: Dec 5, 2015
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    @DMSolace OnTriggerEnter is a unity specific function, you can't just change it's parameters. It specifically takes a singular collider.