Array sorting issues - Please Help!!!
Hello I have an issue with this script. I have 8 GameObjects, which each share a common script called "Waypoint Progress Tracker" (C#) attached to them, with a public variable "progressDistance" in the script. I have a second script called "Array" (Js) which has the following information in it:
var Cars : GameObject[];
function Update ()
{
System.Array.Sort(Cars, CompareCondition);
}
function CompareCondition(carA: GameObject, carB: GameObject): int
{
var scriptA = carA.GetComponent("WaypointProgressTracker");
var scriptB = carB.GetComponent("WaypointProgressTracker");
if (scriptA.progressDistance > scriptB.progressDistance) return -1;
if (scriptA.progressDistance < scriptB.progressDistance) return 1;
return 0;
}
Everything works up until the if-statements. I get the error:
BCE0019: 'progressDistance' is not a member of 'UnityEngine.Component'.
Can anyone help me? Im trying to sort (ascending ) the array of GameObjects based on the "progressDistance" variables.
Answer by SwagGamez · Dec 28, 2015 at 03:58 AM
Ok I finally figured it out lol. I just took out "#pragma strict" and it now works perfectly with the script I originally posted. Geesh all that time that line just needed to be deleted smh. Thank you two for your help though!!!
Answer by IgorAherne · Dec 27, 2015 at 02:44 PM
I think it might be similar to this
http://answers.unity3d.com/questions/233651/thrust-is-not-a-member-of-unityenginecomponent.html
Thanx for your quick response. Without the quotations around "WaypointProgressTracker" it gives me errors. I think its because Im trying to access a C# script from a Javascript. So I need the quotations. But its not recognizing the variable "progressDistance".
Answer by allenallenallen · Dec 27, 2015 at 05:24 PM
So scriptA and scriptB are both fine but their variable progressDistance isn't? That's weird. I feel like scriptA and scriptB still shouldn't be reached since you used quotations for GetComponent.
Say, what if it's about the compiler time? Make sure the C# script is compiled before Array.js.
I compiled the scripts in order a few weeks ago because I read somewhere that in order to access a C# script from a Javascript you have to use the quotations and its a certain compiling order plus the C# script has to be inside of Standard Assets folder and JS outside of it. I'm confused because I should be able to access the progressDistance variable.
Answer by LazyElephant · Dec 27, 2015 at 08:50 PM
The GetComponent( string name )
function returns a component object. To make it useable as the specific component you want, you have to explicitly cast it.
var scriptA = carA.GetComponent("WaypointProgressTracker") as WaypointProgressTracker;
Alternatively you could use the generic version GetComponent.<WaypointProgressTracker>()
I've tried both of your suggestions but I get errors with those lines. It's frustrating because I'm trying to create a car position ranking system (1st, 2nd, 3rd, etc) then display it on a GUI. Its the last thing I need to complete my game and I've been trying at this for months.