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
1
Question by stormcloak · Jan 08, 2015 at 05:37 AM · arraytilearray of gameobjects

Sorting an array of GameObjects based on its name.

I'm making a 2d board game for which I want to implement player movement along tiles based on a dice roll. The problem is when I create an array for the tiles, it's stored in a random order. For eg: the debug log of tiles[0] returns gameobject tile 12 instead of gameobject tile0. How do I sort this array so that I get tiles[0] returns tile0, tiles[1] returns tile1.....etc.

 using UnityEngine;
 using System.Collections;
 
 public class Tiles : MonoBehaviour {
     //GameObject Alltiles is a parent of other gameobjects such as tile1,tile2,tile3....tile23.
     public GameObject Alltiles;
     //creating an array named tiles to store all other tile gameobjects.
     public GameObject[] tiles;
     // Use this for initialization
     void Start () {
         tiles = GameObject.FindGameObjectsWithTag("tile");
         System.Array.Sort(tiles);
         Debug.Log(tiles[0]);
         Debug.Log(tiles[1]);
         Debug.Log(tiles[2]);
         Debug.Log(tiles[3]);
         Debug.Log(tiles[4]);
         Debug.Log(tiles[5]);
     }
     
     // Update is called once per frame
     void Update () {
     
     }
 }
 
Comment
Add comment · Show 1
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 MiniForge · Jan 08, 2015 at 07:00 AM 0
Share

You could retrieve the objects name with GameObject[0].name then use the string compare method. http://msdn.microsoft.com/en-us/library/84787k22(v=vs.110).aspx Then depending on the value returned you would swap array elements!

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by stormcloak · Jan 10, 2015 at 03:44 AM

The above two answers didn;t work but the one from this thread worked perfectly fine! http://forum.unity3d.com/threads/c-array-sorting-horror-solved.24077/

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 thegreatzebadiah · Jan 08, 2015 at 07:38 AM

I would use a sorted dictionary. Just insert each object with the name as the key like this:

 SortedDictionary<string, GameObject> sortedTiles = new SortedDictionary<string, GameObject>();
 for (int i = 0; i < tiles.Length; i++)
 {
     sortedTiles.Add(tiles[i].name, tiles[i]);
 }

then to iterate through in sorted order you could:

 foreach(KeyValuePair<string, GameObject> tile in sortedTiles)
 {
     // Do something with tile
 }

or if you want to get back to an array and have convenient index access:

 tiles = sortedTiles.Values.ToArray();


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 Mangas · Jan 08, 2015 at 07:37 AM

Perhaps it's easier to just set up the array in the correct order rather than initializing it, assigning all gameobjects and then sorting it.

If your gameobjects are named as tile0, tile1, tile2... etc. Try to make a loop where you assign each tile like this:

tiles[i] = GameObject.FindGameObjectsWithTag("tile" + i)

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 stormcloak · Jan 10, 2015 at 03:43 AM 0
Share

This method didn't work.

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

28 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

Related Questions

How to Instantiate into a terrain array? 2 Answers

Spawning GameObjects with help of classes --- Attaching classes to GameObjects 1 Answer

Call function only on the Object the player is interacting with. (when there are multiple copys of the object) 1 Answer

List of bools assigned to each Gameobject in List/Array 0 Answers

Get all GameObjects by variable value 2 Answers


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