Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 /
avatar image
0
Question by Imagineer · Apr 28, 2014 at 02:42 PM · arraytagorderwrong

Array loads in wrong order

Hey all, I want to load GameObjects in an array with the following code. The problem is that the array is loaded in reversed order, so the first gamobject in de hierarchy is the last one in the array. Is there a way to change this??

         public GameObject[] trackPoints;
         public float distanceToNextTrackPoint;
         public int currentTrackPoint = 0;
 
     void Start ()    
      {
         //If points can be found, calculate distance to first point
         trackPoints = GameObject.FindGameObjectsWithTag("TrackPoint");
         if(trackPoints != null)
         {
             distanceToNextTrackPoint = Vector3.Distance(transform.position, trackPoints[currentTrackPoint].transform.position);
         }
Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by flaviusxvii · Apr 28, 2014 at 02:44 PM

You shouldn't have any expectations about the order of Objects returned by GameObject.FindGameObjectsWithTag("TrackPoint"). That is completely beyond your control.

If you want them ordered a certain way, you'll need to sort them after you retrieve them.

Comment
Add comment · Show 2 · 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 Imagineer · Apr 28, 2014 at 03:22 PM 0
Share

Okay, but the array is exactly the wrong way, so isn't there a way to "mirror" the array??

avatar image flaviusxvii · Apr 28, 2014 at 04:46 PM 0
Share

http://www.dotnetperls.com/array-reverse

But HEED $$anonymous$$Y WARNING! I don't see any reason to assume FindGameObjectsWithTag will continue to work this way. It is a mistake to continue with this.

avatar image
0

Answer by teslatron · Apr 28, 2014 at 02:57 PM

After getting your objects you can sort them according to their names, like:

 Array.Sort(trackPoints , (x, y) => x.name.CompareTo(y.name));
   

Or if you have more complex sorting logic then you can create your own IComparer class:

 /// <summary>
 /// Object sorter optimized to use like this: 
 /// Object_1
 /// Object_2
 /// Object_3
 /// Object_4
 /// </summary>
 public class ObjectSorter : IComparer
 {
     int IComparer.Compare(object x, object y)
     {
         string xName = ((GameObject)x).name;
         string yName = ((GameObject)y).name;
 
         if (xName.Contains("_") && yName.Contains("_")) 
         {
             int a = int.Parse(xName.Split('_')[1]);
             int b = int.Parse(yName.Split('_')[1]);
 
             return a - b;
         }
         else 
         {
             // Format error but we sort it to the name anyway
             return xName.CompareTo(yName);
         }
     }
 }

And use it like this:

 // This goes to your definitions field
 private IComparer mObjectSorter = new ObjectSorter();
     
 ..
 ..
     
 // In your method
 Array.Sort(mObjects, trackPoints);
Comment
Add comment · 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
0

Answer by KillMobil · Apr 28, 2014 at 02:58 PM

Ive encountered a similar problem and could find a solution or understanding of the sorting in ther array.

initially i worked with a predifined name for the track points along with an incremented id ex: Point_0, Point_1, Point_2, Point_3,...

and then interate throung the Containing GameObject

 for (int i = 0; i < wayPointContainer.childCount; i++){
   GameObject wayPointGO = wayPintContainer.Find( "Point" +  (i) );
   wayPointArray.push (wayPointGO);
 }
 

but generally both solutions are a bit of a hassle to maintain.. So i ended up Using the poly line editor Script witch is a very simple editor tool that allow you to create path and get it back as a Vector3 Array.

http://wiki.unity3d.com/index.php/PolyLineEditor

the source is a been out of date so also look at this

http://answers.unity3d.com/questions/330120/polyline-script-form-unify-wiki-help.html

Comment
Add comment · 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

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.

Follow this Question

Answers Answers and Comments

23 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

Related Questions

Assigning tag on runtime break loop in C# unity3D 4.3. 1 Answer

Array Question Length (C#) 8 Answers

How to create an array of Game Objects with different tags. 1 Answer

List and store vector3 of game objects with tag 1 Answer

How can I order an array of RaycastHits in reverse order of distance? 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