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 /
This question was closed Jun 24, 2012 at 08:32 PM by TheDarkVoid for the following reason:

The question is answered, right answer was accepted

avatar image
4
Question by TheDarkVoid · Jun 21, 2012 at 09:04 PM · c#gameobjectarraylistsort

[C#] Sorting a List of Gameobjects Alphabetically

i have this code, it find all gameobjects that are marked as a waypoint and adds it to a list/array.

 using UnityEngine;
 using System.Collections;
 
 public class CreepAI : MonoBehaviour {
     //Vars
     public GameObject Target = null;
     public GameObject[] WayPoints;
     void Awake () {
         FindWayPoints();
     }
     void FindWayPoints()
     {
         WayPoints = GameObject.FindGameObjectsWithTag("Waypoint");
         
     }
     void LateUpdate () {
         if(Target = null)
             Target = FindClosestEnemy();
         FindWayPoints();
     }
      
 }

When i run the code the gameobjects are arranged in a way that makes no sense, they are not arranged in the world this way or in the hierarchy this way.

Gameobject List

i want to be able to sort this list, so Wp0 will be first and Wp7 will be last in order.

Untitled.png (10.0 kB)
Comment
Add comment · Show 2
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 kersk · Jun 21, 2012 at 09:42 PM 1
Share

It looks like you have a bug in your LateUpdate(), you probably meant to write "if(Target == null)" ins$$anonymous$$d of assigning Target = null every frame.

avatar image TheDarkVoid · Jun 22, 2012 at 02:02 AM 0
Share

the part of the code is for something else that i cut out to save space when i pasted the code here. it being = and not == was a mistake, thanks for pointing it out.

2 Replies

  • Sort: 
avatar image
24
Best Answer

Answer by Berenger · Jun 21, 2012 at 09:19 PM

Try (tested and approved) :

 using System.Linq;

// ...

 void FindWayPoints()
 {
    WayPoints = GameObject.FindGameObjectsWithTag("Waypoint").OrderBy( go => go.name ).ToArray();
 }
Comment
Add comment · Show 7 · 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 TheDarkVoid · Jun 21, 2012 at 09:22 PM 0
Share

i will try this,........ it worked

avatar image TheDarkVoid · Jun 21, 2012 at 09:22 PM 0
Share

thanks for ur help

avatar image whydoidoit · Jun 21, 2012 at 09:48 PM 0
Share

@berenger nice :)

avatar image Berenger · Jun 21, 2012 at 09:55 PM 0
Share

@whydoidoit I knew you would like it :p

avatar image Aldo · Jan 11, 2013 at 05:54 PM 0
Share

THAN$$anonymous$$S A LOT!!!!! and yup Im yelling of happyness I tried many things but this is simply perfect!!

Show more comments
avatar image
1

Answer by kersk · Jun 21, 2012 at 09:52 PM

After you grab all of the waypoints in FindWayPoints(), sort the WayPoints array by the waypoint's gameobject name.

Just to get you going, you can use Array.Sort -- check out MSDN

Try something like:

 Array.Sort(WayPoints, delegate(GameObject wp1, GameObject wp2) { return wp1.name.CompareTo(wp2.name); });

(I didn't test that - no guarantees on that compiling!)

However, Array.Sort and GameObject.FindGameObjectsWithTag are both very slow. Probably not something you want to be doing everytime you create a new NPC. I'd recommend considering switching over to creating a gameobject named something like "WaypointRegistry" that also has a "WaypointRegistry" script that implements a singleton -- that script would do the work once to maintain your sorted list of waypoints in the scene, and then provide static and centralized access to this list. Your creeps could simply call something like "Waypoints = WaypointRegistry.GetAllWaypoints();"

It also looks like you have a bug in your LateUpdate(), you probably meant to write "if(Target == null)" instead of assigning Target = null.

Hope this helped!

Comment
Add comment · 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 TheDarkVoid · Jun 21, 2012 at 10:24 PM 0
Share

thanks for the advice, i will defiantly use the registry idea.

Follow this Question

Answers Answers and Comments

7 People are following this question.

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

Related Questions

How to put gameObjects to the list? 4 Answers

What is the best way to instatiate an array of connected GameObjects? 0 Answers

Can't Activate a GameObject from Array 1 Answer

How would I find the name of all game objects in a c# List 1 Answer

Order a GameObject array based on an int property of each object? 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