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

How do you access components of a array with game objects?

Discussion in 'Scripting' started by F35Lightning, May 27, 2016.

  1. F35Lightning

    F35Lightning

    Joined:
    Apr 23, 2014
    Posts:
    3
    Hey guys im trying to make a script where the code takes all the objects in the game and deals 100 damage to them. I use an array to get all the objects however i dont know how to get all their components at once and deal damage to them. If you guys could help it would be much appreciated. Thanks Beforehand :)

    Code:

    Code (JavaScript):
    1. var explosion : GameObject;
    2. var BlastRadius : int = 10;
    3. var Damage : int = 100;
    4. var Radius : float = 10.0;
    5. var Enemy : AIdamage;
    6. var Enemies : GameObject[];
    7. private var center : Vector3;
    8.  
    9. private var hitTarget  : GameObject;
    10.  
    11. function update (){
    12.  
    13.     Enemies = GameObject.FindGameObjectsWithTag("Enemy");
    14.  
    15. }
    16.  
    17. function OnCollisionEnter(hitTarget : UnityEngine.Collision)
    18. {
    19.  
    20.      Enemies.GetComponents("AIdamage").CurrentHealth -= Damage;
    21.  
    22.  
    23.  
    24. }
    25.  
    26.  
     
  2. x3r

    x3r

    Joined:
    May 25, 2016
    Posts:
    46
    Try foreach()...that should do it
     
    Kiwasi likes this.
  3. F35Lightning

    F35Lightning

    Joined:
    Apr 23, 2014
    Posts:
    3
    Sry to bother but I tried and messed it up could you please give me an example of how i can implement it?
     
  4. jaasso

    jaasso

    Joined:
    Jun 19, 2013
    Posts:
    64
    c# example of how to access the AIDamage classes and check for hits with Physics.OverlapSphere:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ExplosionExample : MonoBehaviour
    5. {
    6.     public GameObject explosionEffect;
    7.     public float damage=5, blastRadius=10;
    8.  
    9.     bool firstCollision=true;
    10.  
    11.     void OnCollisionEnter(Collision col)
    12.     {
    13.         if(firstCollision)
    14.         {
    15.             firstCollision=false;
    16.  
    17.             Collider[] hitColliders = Physics.OverlapSphere(transform.position, blastRadius);
    18.  
    19.             for(int i=0;i<hitColliders.Length;i++)
    20.             {
    21.                 if(hitColliders[i].CompareTag("Enemy"))
    22.                 {
    23.                     hitColliders[i].GetComponent<AIDamage>().CurrentHealth -= damage;
    24.                 }
    25.             }
    26.         }
    27.  
    28.         //explosionEffect
    29.     }
    30. }
    if you want to avoid using GetComponent too much for some reason, one way to do so is to use a dictionary from System.Collections.Generic to store the AIDamage classes

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class ExplosionExample2 : MonoBehaviour
    6. {
    7.     public GameObject explosionEffect;
    8.     public float damage=5, blastRadius=10;
    9.  
    10.     Dictionary<Collider,AIDamage>enemyScripts=new Dictionary<Collider, AIDamage>();
    11.  
    12.     void Start ()
    13.     {
    14.         GameObject[] hitTargets;
    15.         hitTargets=GameObject.FindGameObjectsWithTag("Enemy");
    16.  
    17.         for(int i=0; i<hitTargets.Length; i++)
    18.         {
    19.             enemyScripts.Add(hitTargets[i].GetComponent<Collider>(),hitTargets[i].GetComponent<AIDamage>());
    20.         }
    21.     }
    22.  
    23.     bool firstCollision=true;
    24.  
    25.     void OnCollisionEnter(Collision col)
    26.     {
    27.         if(firstCollision)
    28.         {
    29.             firstCollision=false;
    30.  
    31.             Collider[] hitColliders = Physics.OverlapSphere(transform.position, blastRadius);
    32.  
    33.             for(int i=0;i<hitColliders.Length;i++)
    34.             {
    35.                 if(hitColliders[i].CompareTag("Enemy"))
    36.                 {
    37.                     enemyScripts[hitColliders[i]].CurrentHealth -= damage;
    38.                 }
    39.             }
    40.         }
    41.  
    42.         //explosionEffect
    43.     }
    44. }
     
    F35Lightning likes this.
  5. F35Lightning

    F35Lightning

    Joined:
    Apr 23, 2014
    Posts:
    3
    Thanks for the help but would it still work in Javascript? Since im not very good with C# sry :(
    Im kind of new to Unity :/

    Also, Whats with the first collision bollean?
     
    Last edited: May 27, 2016