Hi, I want to store the name of the gameObjects i instantiated to an array list according to its position. What I currently have is store the game objects to array according to the time that the objects are created. The first instantiated object is stored automatically to an array[0] and the 2nd instantiated objet is stored in array[1].
But i want to modify the list. Say I have a “manage objects” button, and when i click it, i can drag the objects from one location to another. Then when i click the button “deploy”, the objects currently in the area should be stored to an array according the its position from left to right. The object in the left corner should be stored in array[0] then the object in the right side of that object should be stored in array[1] and so on…
I hope someone could help me figure out how it should be done. thanks.
I’m trying to use the hashtable so my key would be the gameObject’s position and value would be its name then put all the keys of my hashtable to a list then sort that list. After sorting the list, I’m thinking of putting those sorted floats back to the hashtable, but the value should be the values corresponding to its values when it was not yet sorted.
But I’m stucked with putting back the values to the hash table after sorting. Here’s my code:
if (GUI.Button(new Rect(0, Screen.height / 2, 100, 30), "Deploy"))
{
List<float> ListOfPosition = new List<float>();
GameObject[] ArrayOfCards = GameObject.FindGameObjectsWithTag("Card");
Hashtable cardsPosAndName = new Hashtable();
string cardname;
foreach (GameObject card in ArrayOfCards)
{
cardPosX = card.transform.position.x;
cardname = card.name;
cardsPosAndName.Add(cardPosX, cardname);
}
foreach(float Key in cardsPosAndName.Keys)
{
ListOfPosition.Add(Key);
}
ListOfPosition.Sort();
//THIS IS WHERE I'M STUCKED.
}
Okay, got it correctly working by some researches.
Here’s my code:
if (GUI.Button(new Rect(0, Screen.height / 2, 100, 30), "Deploy"))
{
//List<float> ListOfPosition = new List<float>();
GameObject[] ArrayOfCards = GameObject.FindGameObjectsWithTag("Card");
Hashtable cardsPosAndName = new Hashtable();
ArrayList ArrayOfSortedCards = new ArrayList();
string cardname;
foreach (GameObject card in ArrayOfCards)
{
cardPosX = card.transform.position.x;
cardname = card.name;
cardsPosAndName.Add(cardPosX, cardname);
}
ArrayList ListOfPosition = new ArrayList(cardsPosAndName.Keys);
ListOfPosition.Sort();
foreach (float position in ListOfPosition)
{
if (cardsPosAndName.ContainsKey(position))
{
string value = (string)cardsPosAndName[position];
ArrayOfSortedCards.Add(value);
}
}
foreach (string cardToDeploy in ArrayOfSortedCards)
{
print(cardToDeploy);
}
}
If you have any suggestions or comments, I would appreciate. 