Search Unity

Help with returning an Array.

Discussion in 'Scripting' started by psycocrusher, Jan 30, 2015.

  1. psycocrusher

    psycocrusher

    Joined:
    Jul 24, 2011
    Posts:
    71
    Hi,

    I want this script to return all enemies within a certain distance, but it doesn't return an array, can anyone help me with this.
    Code (CSharp):
    1.     public Transform[] Target(float maxDistance ){
    2.        
    3.         foreach(GameObject Vehicule in GameObject.FindGameObjectsWithTag("MissileTarget")){
    4.            
    5.             //Find target distance.
    6.             Vector3 relativePosition = Vehicule.transform.position - ThePlayer.transform.position;
    7.            
    8.             if (relativePosition.magnitude < maxDistance){
    9.                
    10.                 return Vehicule.transform;
    11.             }
    12.         }
    13.        
    14.         return null;
    15.     }
    Thank you.
     
  2. Defero

    Defero

    Joined:
    Jul 9, 2012
    Posts:
    200
    Your script currently returns null or transform. But the expect return of the function is transform array (Transform[]).

    It's probably best, because you need a dynamic array, to use an ArrayList here.

    Code (CSharp):
    1.     public ArrayList<Transform> Target(float maxDistance ){
    2.  
    3.         ArrayList<Transform> arr = new ArrayList<Transform>();
    4.  
    5.         foreach(GameObject Vehicule in GameObject.FindGameObjectsWithTag("MissileTarget")){
    6.          
    7.             //Find target distance.
    8.             Vector3 relativePosition = Vehicule.transform.position - ThePlayer.transform.position;
    9.          
    10.             if (relativePosition.magnitude < maxDistance){
    11.              
    12.                 arr.Add (Vehicule.transform);
    13.             }
    14.         }
    15.      
    16.         return arr;
    17.     }
     
  3. Defero

    Defero

    Joined:
    Jul 9, 2012
    Posts:
    200
    I don't know often you are going to call this function, but it might be worth mentioning that the following call is not "fast".
     
  4. psycocrusher

    psycocrusher

    Joined:
    Jul 24, 2011
    Posts:
    71
    Thanks for the input.
     
  5. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    You could use a List, basically a fancy array.

    Add "using System.Collections.Generic" to the top of your script, then do
    Code (CSharp):
    1. List<Transform> tL = new list<Transform>();
    2. tL.Add(this.transform); //example
    3. return tL.ToArray();
    4.  
    5.  
     
  6. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    ZO5KmUG6R and Defero like this.
  7. DryTear

    DryTear

    Joined:
    Nov 30, 2012
    Posts:
    312
    Code (csharp):
    1.  
    2.     public Transform[] GetTransformsInRange(float radius, Vector3 position, string specificTag)
    3.     {
    4.         List<Transform> transformsToReturn = new List<Transform>();
    5.         Collider[] cols = Physics.OverlapSphere(position, radius);
    6.         for(int i = 0; i < cols.Length;i++)
    7.         {
    8.             if(cols[i].gameObject.CompareTag(specificTag))
    9.             {
    10.                 transformToReturn.Add(cols[i].transform);
    11.             }
    12.         }
    13.         return transformsToReturn;
    14.     }
    15.  
    if you get errors with the List, make sure you have using System.Collections.Genereic;

    usage is enemyTransforms = GetTransformsInRange(3f, transform.position, "Enemy");