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

How to get objects with a specific component and store them in an array?

Discussion in 'Scripting' started by Sabudum, Nov 27, 2014.

  1. Sabudum

    Sabudum

    Joined:
    Nov 18, 2014
    Posts:
    11
    So, i made this code to switch weapons, and all i'm trying to do is get how much weapons the player has, so i know how to set the array, and then, put all weapons into the array, however, how do i get the objects with the "WeaponFire" component? So that i don't need to set the array length before running the game, because i wont always have the same number of weapons.

    BTW, this is not an FPS, nor a TPS, it is a car combat game.

    Code:
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var currentWeapon = 0;
    4. var Weapons : WeaponFire[];
    5.  
    6. function getSelectedWeapon() : WeaponFire
    7.     {
    8.     return Weapons[currentWeapon];
    9.     }
    10.  
    11.  
    12. function Start()
    13.     {
    14.    
    15.     if (currentWeapon >= Weapons.length) currentWeapon = Weapons.length-1;
    16.    
    17.     for (var i=0; i<Weapons.length; i++)
    18.         DisableWeapon(Weapons[i]);
    19.    
    20.     SelectWeapon(Weapons[currentWeapon]);
    21.     }
    22.  
    23.  
    24. function Update () {
    25.    
    26.     if (Input.GetKeyDown(KeyCode.PageUp))
    27.         SwitchWeapon(-1);
    28.        
    29.     if (Input.GetKeyDown(KeyCode.PageDown))
    30.         SwitchWeapon(1);
    31. }
    32.  
    33. function SwitchWeapon (iDir : int)
    34.     {
    35.     if (Weapons.length < 2) return;
    36.    
    37.     // Desactivar coche previo
    38.    
    39.     DisableWeapon(Weapons[currentWeapon]);
    40.        
    41.     // Seleccionar coche nuevo
    42.    
    43.     currentWeapon += Mathf.Sign(iDir);
    44.     if (currentWeapon < 0) currentWeapon = Weapons.length-1;
    45.     else if (currentWeapon >= Weapons.length) currentWeapon = 0;
    46.    
    47.     SelectWeapon(Weapons[currentWeapon]);
    48.     }
    49.    
    50.    
    51. // Desactivar el coche dado. No depende de variables globales.
    52.    
    53. function DisableWeapon(Weapon : WeaponFire)
    54.     {
    55.     Weapon.weaponSelected = false;
    56.     }
    57.  
    58.    
    59. // Selecciona el coche dado. Lo activa y obtiene sus componentes.
    60.  
    61. function SelectWeapon(Weapon : WeaponFire)
    62.     {
    63.     Weapon.weaponSelected = true;
    64.     }
    65.  
    66.  
     
  2. rkaetano

    rkaetano

    Joined:
    Aug 27, 2012
    Posts:
    61
  3. Sabudum

    Sabudum

    Joined:
    Nov 18, 2014
    Posts:
    11
    Uhm, that's not it, i'm trying to create an array of all objects that contain a specific component, and not trying to get a component from one single object.
     
  4. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    You can use
    Code (CSharp):
    1. MyComponent[] myComponents = GameObject.FindObjectsOfType<MyComponent>();
    It's a slow function though, so best to only use it at load time, not every frame
     
  5. Sabudum

    Sabudum

    Joined:
    Nov 18, 2014
    Posts:
    11
    Sorry to ask, but, how do i do that with Javascript? And, no problem being slow for this script, as it is just for switching weapons.

    Any way to use that with my corrent script, or i will have to re-write all of it?
     
    Last edited: Nov 27, 2014
  6. AlucardJay

    AlucardJay

    Joined:
    May 28, 2012
    Posts:
    328
    Last edited: Nov 28, 2014
  7. Sabudum

    Sabudum

    Joined:
    Nov 18, 2014
    Posts:
    11
    Thank you all guys, i'll fiddle with this and see what i can get.
     
  8. Sabudum

    Sabudum

    Joined:
    Nov 18, 2014
    Posts:
    11
    I ended up creating a very different setup for dynamic weapons, i simply created an empty and i attach all weapons to this empty, the script then counts how many child objects this empty has, and sets the array based on this. If anyone is having trouble with a similar issue, i'll elave the code here:

    Code (JavaScript):
    1.     #pragma strict
    2.    
    3.     var currentWeapon = 0;
    4.    
    5.     function Awake(){
    6.         SelectWeapon(0);
    7.     }
    8.    
    9.     function Update(){
    10.     var maxWeapons : int = transform.childCount - 1;
    11.    
    12.     if(Input.GetAxis("Mouse ScrollWheel") > 0){
    13.         if(currentWeapon + 1 <= maxWeapons){
    14.             currentWeapon++;
    15.         }
    16.         else{
    17.             currentWeapon = 0;
    18.         }
    19.         SelectWeapon(currentWeapon);
    20.     }
    21.     else if (Input.GetAxis("Mouse ScrollWheel") < 0){
    22.         if(currentWeapon - 1 >= 0){
    23.             currentWeapon--;
    24.         }
    25.         else{
    26.             currentWeapon = maxWeapons;
    27.         }
    28.         SelectWeapon(currentWeapon);
    29.     }
    30.    
    31.     if(currentWeapon == maxWeapons + 1){
    32.         currentWeapon = 0;
    33.     }
    34.     if(currentWeapon == -1){
    35.         currentWeapon = maxWeapons;
    36.     }
    37. }
    38.  
    39. function SelectWeapon (index : int){
    40.     for (var i = 0; i < transform.childCount; i++){
    41.  
    42.         if (i == index){
    43.             transform.GetChild(i).gameObject.GetComponent(WeaponFire).weaponSelected = true;
    44.         }
    45.         else{
    46.             transform.GetChild(i).gameObject.GetComponent(WeaponFire).weaponSelected = false;
    47.         }
    48.     }
    49. }