x


[Closed] [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)
more ▼

asked Jun 21 '12 at 09:04 PM

TheDarkVoid gravatar image

TheDarkVoid
956 13 23 28

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

Jun 21 '12 at 09:42 PM kersk

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.

Jun 22 '12 at 02:02 AM TheDarkVoid
(comments are locked)
10|3000 characters needed characters left

The question has been closed Jun 24 '12 at 08:32 PM by TheDarkVoid for the following reason:

The question is answered, right answer was accepted


2 answers: sort voted first

Try (tested and approved) :

using System.Linq;

// ...

void FindWayPoints()
{
   WayPoints = GameObject.FindGameObjectsWithTag("Waypoint").OrderBy( go => go.name ).ToArray();
}
more ▼

answered Jun 21 '12 at 09:19 PM

Berenger gravatar image

Berenger
11.2k 12 19 54

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

Jun 21 '12 at 09:22 PM TheDarkVoid

thanks for ur help

Jun 21 '12 at 09:22 PM TheDarkVoid

@berenger nice :)

Jun 21 '12 at 09:48 PM whydoidoit

@whydoidoit I knew you would like it :p

Jun 21 '12 at 09:55 PM Berenger

THANKS A LOT!!!!! and yup Im yelling of happyness I tried many things but this is simply perfect!!

Jan 11 at 05:54 PM Aldo
(comments are locked)
10|3000 characters needed characters left

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!

more ▼

answered Jun 21 '12 at 09:52 PM

kersk gravatar image

kersk
1 1

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

Jun 21 '12 at 10:24 PM TheDarkVoid
(comments are locked)
10|3000 characters needed characters left

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x4372
x2171
x1396
x382
x61

asked: Jun 21 '12 at 09:04 PM

Seen: 2299 times

Last Updated: Jun 01 at 08:42 PM