• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
Question by SwagGamez · Dec 27, 2015 at 02:03 PM · arrayarraysarray of gameobjectssortingsort

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.

Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

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!!!

Comment
MystoganY

People who like this

1 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image

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

Comment
MystoganY

People who like this

1 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SwagGamez · Dec 27, 2015 at 03:40 PM 0
Share

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".

avatar image

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.

Comment
MystoganY

People who like this

1 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SwagGamez · Dec 28, 2015 at 03:22 AM 0
Share

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.

avatar image

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>()

Comment
MystoganY

People who like this

1 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SwagGamez · Dec 28, 2015 at 03:31 AM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

32 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to compare a position of an object within an array 1 Answer

Moving spawned enemies randomly towards several pre-defined positions 1 Answer

Storing a Gameobject from array into a Gameobject variable giving NullReferenceException 3 Answers

Having problems with custom objects in my array - BCE0019 0 Answers

How to change another object's Tag 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges