Search Unity

Trying to sort an Array

Discussion in 'Scripting' started by RoryScanlan, Aug 31, 2014.

  1. RoryScanlan

    RoryScanlan

    Joined:
    Jan 25, 2014
    Posts:
    139
    So im tryin to sort these arrays so they corispond with each other but im getting an error about static methods that i dont understand :(
    Do i need to create a static method or something? :/ Any help will be lovely.
    Im a new to coding so have mercy :D
    Here is the code
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5.  
    6. public class PositionScript : MonoBehaviour {
    7.  
    8.     public List<GameObject> objects = new List<GameObject> ();
    9.     public List<float> distances = new List<float> ();
    10.     public GameObject[] carNames;
    11.  
    12.     private bool updateCars = true;
    13.  
    14.  
    15.     void Update()
    16.     {
    17.         if (updateCars)
    18.         {
    19.             StartCoroutine(UpdateDistance());
    20.         }
    21.     }
    22.  
    23.     IEnumerator UpdateDistance()
    24.     {
    25.         updateCars = false;
    26.         yield return new WaitForSeconds(1.0f);
    27.         distances.Clear ();
    28.         foreach (GameObject obj in objects) {
    29.             float dist = Vector3.Distance (gameObject.transform.position, obj.transform.position);
    30.             distances.Add (dist);
    31.         }
    32.        
    33.         distances.Sort ();
    34.         float[] distance = distances.ToArray ();
    35.         carNames = objects.ToArray ();
    36.  
    37.         carNames.Sort (distance, carNames);
    38.         updateCars = true;
    39.     }
    40. }
    41.  
    42.  
     
    Last edited: Aug 31, 2014
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Line 37: Sort works without parameters (like line 33), but unless you provide a comparison method, you can't just stick parameters in it.
     
    Polymorphik likes this.
  3. RoryScanlan

    RoryScanlan

    Joined:
    Jan 25, 2014
    Posts:
    139
    Ahhhh sorry i thaught i could sort the second list using the first list. In the end i managed to acheive what i was looking for with a slightly different approach.

    If this helps anyone for the begining of a 'race position' script then go ahead and grab it here is the code :)
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class PositionScript : MonoBehaviour {
    6.  
    7.     public List<GameObject> objects = new List<GameObject> ();
    8.     public List<float> carPositions = new List<float> ();
    9.  
    10.     private bool updateCars = true;
    11.  
    12.  
    13.     void Update()
    14.     {
    15.         if (updateCars)
    16.         {
    17.             StartCoroutine(UpdateDistance());
    18.         }
    19.     }
    20.  
    21.     IEnumerator UpdateDistance()
    22.     {
    23.         updateCars = false;
    24.         yield return new WaitForSeconds(0.1f);
    25.         carPositions.Clear ();
    26.         foreach (GameObject obj in objects) {
    27.             float dist = Vector3.Distance (gameObject.transform.position, obj.transform.position);
    28.             carPositions.Add(dist);
    29.         }
    30.        
    31.         carPositions.Sort ();
    32.         updateCars = true;
    33.     }
    34. }
    35.  
    and the second script
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CarIdentScript : MonoBehaviour {
    5.  
    6.     public int CurrentPosition;
    7.     public string CarName;
    8.     public GameObject RaceController;
    9.  
    10.     private PositionScript positionScript;
    11.  
    12.     void Start()
    13.     {
    14.         positionScript = RaceController.GetComponent<PositionScript> ();
    15.     }
    16.  
    17.     void Update()
    18.     {
    19.         float dist = Vector3.Distance (RaceController.transform.position, gameObject.transform.position);
    20.         CurrentPosition = Mathf.Abs(positionScript.carPositions.BinarySearch (dist));
    21.     }
    22. }
    23.  
     
    Last edited: Aug 31, 2014