How to order RaycastAll by distance on IOS

Hi.
I’m working on a code that selects elements, if clicked on coloured pixel. As described in one of my [other question here][1]

Here’s the current code I have and it works. But the problem is that the current method of ordering RaycastAll does not work on IOS.

using UnityEngine;
using System.Collections;
using System.Linq;
using System.Collections.Generic;

public class AlphaTest : MonoBehaviour {
	
	private RaycastHit[] hits;
	private Ray ray;
	Camera myCam;
	private GameObject selectedPlayer = null;
	List<GameObject> disabledGameobjects = new List<GameObject>();
	
	// Use this for initialization
	void Start () {
	myCam=Camera.main;
	}
 
    void Update() {
		ray = myCam.ScreenPointToRay(Input.mousePosition);
		//When mouse left button is pressed down | This Fires only once
        if(Input.GetMouseButtonDown(0)){
	        	
	       		hits = Physics.RaycastAll(ray).OrderBy(h=>h.distance).ToArray();//Order raycastall by hit distance
				for(int i=0;i<hits.Length;i++)
				{	
					
					if(hits*.collider.tag=="Player")*
  •  			{*
    

_ selectedPlayer = hits*.collider.transform.gameObject;*_

_ Renderer renderer = hits*.collider.renderer;
MeshCollider meshCollider = hits.collider as MeshCollider;
if (renderer == null || renderer.sharedMaterial == null || renderer.sharedMaterial.mainTexture == null || meshCollider == null)
return;*_

* Texture2D texture = renderer.material.mainTexture as Texture2D;*
_ Vector2 pixelUV = hits*.textureCoord;*
pixelUV.x *= texture.width;
pixelUV.y *= texture.height;_

* Color pixel = texture.GetPixel((int)pixelUV.x, (int)pixelUV.y);*
* Debug.Log(pixel.a);*
* if(pixel.a < 1){*
* //We hit a transparent pixel*
* selectedPlayer.renderer.material.color = Color.black;*
* selectedPlayer.collider.enabled=false;*
* disabledGameobjects.Add(selectedPlayer);*
* }*
* else if(pixel.a >= 1){*
* //We did not hit a transparent pixel*
* selectedPlayer.renderer.material.color = Color.white;*
* Debug.Log(selectedPlayer.name);*
* return;*
* }*

* }*

* } *

* }*
* //While mouse left button is held down| This Fires every frame*
* if(Input.GetMouseButton(0)){*

* }*
* //When mouse left button is released | This Fires only once*
* if(Input.GetMouseButtonUp(0)){*
* foreach(GameObject disabledGameobject in disabledGameobjects){*
* disabledGameobject.collider.enabled=true;*
* disabledGameobject.renderer.material.color=Color.white;*
* }*
* disabledGameobjects.Clear();*

* }*

}

}
It just gives me the following:
ExecutionEngineException: Attempting to JIT compile method ‘System.Linq.OrderedEnumerable`1:GetEnumerator ()’ while running with --aot-only.
I have done some research and I found that IOS dosn’t like OrderBy on Arrays. So my first thought was to convert the array hits to a generic List, then order the List and then convert the ordered List back to a array.
Something Like this
hits= Physics.RaycastAll(ray);
Listhitlist = new List(hits).OrderBy(h=>h.distance).ToList();
hits=hitlist.ToArray();
Which worked, but the error remained on IOS.
So how can I order my raycastall by distance so that it would also work on IOS?
_*[1]: http://answers.unity3d.com/questions/545519/best-way-to-click-on-a-object-behind-other.html*_

I think I got it. Or at least it seems to be working and not throwing any errors on IOS.
Took my time and tried out some solutions as suggested here: [Sorting builtin arrays - Unity Answers][1]
And here’s my final code:

using UnityEngine;
using System.Collections;
using System.Linq;
using System.Collections.Generic;
using System;
public class AlphaTest : MonoBehaviour {
	
	private RaycastHit[] hits;
	private Ray ray;
	Camera myCam;
	private GameObject selectedPlayer = null;
	List<GameObject> disabledGameobjects = new List<GameObject>();
	List<RaycastResult> hitList = new List<RaycastResult>();
	
	// Use this for initialization
	void Start () {
	myCam=Camera.main;
	}
	
	
	public class RaycastResult: IComparable<RaycastResult>
	{
	    public float distance;
	    public Collider collider;
		public Vector2 textureCoord;
	    public RaycastResult(RaycastHit hit)
	    {
	        distance = hit.distance;
	        collider = hit.collider;
			textureCoord = hit.textureCoord;
	    }
	 
	    public int CompareTo(RaycastResult other)
	    {
	        return distance.CompareTo(other.distance);
	    }
	}
	
	void SortDistances()
	{
	    hitList.Clear();
	    hits= Physics.RaycastAll(ray);
	 
	    foreach(RaycastHit hit in hits)
	    {
	        hitList.Add(new RaycastResult(hit));
	    }
		hitList.Sort();
	}
	
    void Update() {
		ray = myCam.ScreenPointToRay(Input.mousePosition);
		//When mouse left button is pressed down | This Fires only once
        if(Input.GetMouseButtonDown(0)){
			SortDistances();
			
			Debug.Log("HIT-----------------------------");
			foreach(RaycastHit hit in hits){
				Debug.Log(hit.collider.name+""+hit.distance);
				
			}
			Debug.Log("LIST-----------------------------");
			foreach(RaycastResult listItem in hitList){
				Debug.Log(listItem.distance);
			}
			
			
				for(int i=0;i<hitList.Count;i++)
				{	
					
					if(hitList*.collider.tag=="Player")*
  •  			{*
    

_ selectedPlayer = hitList*.collider.transform.gameObject;*_

_ Renderer renderer = hitList*.collider.renderer;
MeshCollider meshCollider = hitList.collider as MeshCollider;
if (renderer == null || renderer.sharedMaterial == null || renderer.sharedMaterial.mainTexture == null || meshCollider == null)
return;*_

* Texture2D texture = renderer.material.mainTexture as Texture2D;*
_ Vector2 pixelUV = hitList*.textureCoord;*
pixelUV.x *= texture.width;
pixelUV.y *= texture.height;_

* Color pixel = texture.GetPixel((int)pixelUV.x, (int)pixelUV.y);*
* //Debug.Log(pixel.a);*
* if(pixel.a < 1){*
* //We hit a transparent pixel*
* selectedPlayer.renderer.material.color = Color.black;*
* selectedPlayer.collider.enabled=false;*
* disabledGameobjects.Add(selectedPlayer);*
* }*
* else if(pixel.a >= 1){*
* //We did not hit a transparent pixel*
* selectedPlayer.renderer.material.color = Color.white;*
* //Debug.Log(selectedPlayer.name);*
* return;*
* }*

* }*

* } *

* }*
* //While mouse left button is held down| This Fires every frame*
* if(Input.GetMouseButton(0)){*

* }*
* //When mouse left button is released | This Fires only once*
* if(Input.GetMouseButtonUp(0)){*
* foreach(GameObject disabledGameobject in disabledGameobjects){*
* disabledGameobject.collider.enabled=true;*
* disabledGameobject.renderer.material.color=Color.white;*
* }*
* disabledGameobjects.Clear();*

* }*

}

}
_*[1]: http://answers.unity3d.com/questions/22261/sorting-builtin-arrays.html*_

It’s ugly, but you could write your own sorting algorithm: