I'm making a waypoint system. You place GO's on the scene and camera will traverse from one to the next. If I put all waypoint go's in a container object, and loop through it's children, I can get a list of waypoints in order they were added in the hierarchy, which works great. I would also like to have the option to not need a container object. So in this instance I find all waypoint objects with FindObjectsOfType. But this list is not in hierarchy order. Is there any way to sort FindObjectsOfType by hierarchy? Or an alternative method? Thanks
order is not enforceable. You can collect first, then manually sort (eg by name) But I'd stick with keeping them parented, otherwise your looking at a great way to get a very messy scene hierarchy very quickly
I'm not convinced this is true anymore. With 4.6 you can re-order things in the hierarchy and the order controls render depth for UI elements. That being said - relying on order for waypoint pathfinding seems like asking for a lot of trouble.
It's for a package I will be adding to the asset store. I'd like to avoid ordering by name as I can't guarantee how users will rename objects. I will prompt users to create a container object, but just wanted to cover all bases really. I guess I could just throw a warning if no container detected. Thanks
It's a camera waypoint system, just sends the camera from one object to the next, with a few other options (different LookAt for each, pause/speed/callbacks etc). The only alternative I can think of is to set the next waypoint to move to manually, which doesn't feel as intuitive
I'd set them up as an array in whatever script is handling the movement and use the order of that instead.
I do set them up in an array in my handler script... To populate the array I grab the objects from the hierarchy. So, the user adds waypoint prefabs to the scene in editor, and decides what order to go through them by changing their hierarchy order. Then when the game is running it loops through the array. How would you go about populating the array? I think I'm misunderstanding you.
Ahh I see. Kind of feels like an unnecessary extra step though. What would be the advantage, seeing as the waypoints are added to the scene already anyway?
FindObjectsOfType doesn't guarantee any order for the results. You have to define it yourself somehow. You could perhaps use Transform.GetSiblingIndex() for sorting. --Eric
GetSiblingIndex was exactly what I was looking for. Thanks! For future reference, just use this in a custom Sort function... myList.Sort(delegate(GameObject go1, GameObject go2) { return go1.transform.GetSiblingIndex().CompareTo(go2.transform.GetSiblingIndex()); });
I'm here in 2017, still useful though. I don't know that FindObjectsOfType() doesn't guarantee the ordered result.
Nothing in the documentation explicitly states that the order is guaranteed so better to err on the side of caution and assume it doesn't. https://docs.unity3d.com/ScriptReference/Object.FindObjectsOfType.html
This worked for me, for saving all objects into file, always in same order Code (CSharp): Transform[] all_transforms = Object.FindObjectsOfType<Transform>().OrderBy(m => m.transform.GetSiblingIndex()).ToArray(); // Loop through the transforms in the scene, exporting what is necessary foreach (Transform t in all_transforms) { //do save etc. }
For others who see this and get an error, you have to add this to the top of your code Code (CSharp): using System.Linq;
This method will work only for root objects, all other nested objects order will be messed up Root1 - SiblingIndex = 0 -- SubRoot1 - SiblingIndex = 0 -- SubRoot2 - SiblingIndex = 1 -- SubRoot3 - SiblingIndex = 2 Root2 - SiblingIndex = 1 so it this case order will be: SubRoot1 - Root1 - SubRoot2 - Root2 - SubRoot3