- Home /
Hierarchy index of prefab change when putting in into an array?
Hi guys,
How can i make sure that de prefab index in the hierarchy is the same as the array index? I'm making a virtual representation of an installation to manipulate "pixels" in a real life suit. I tried like this to make a string of values inside prefabs.
void Start()
{
OpenConnection();
peltiersObjects = GameObject.FindGameObjectsWithTag("Peltier");
InvokeRepeating("sendInfo", 0.0f, 0.1f);
}
void Update()
{
}
void sendInfo()
{
for (int i = 0; i < peltiers.Length; i++)
{
peltierInfo += ",";
peltierInfo += peltiers[i].GetComponent<Peltier>().hot.ToString();
peltierInfo += ",";
peltierInfo += peltiers[i].GetComponent<Peltier>().temp.ToString("D3");
}
Debug.Log(peltierInfo);
sp.WriteLine(peltierInfo);
sp.BaseStream.Flush();
peltierInfo = "";
}
But the position in the string created is not the same as the position in the string, and that is very important for my project. for Example if the prefabs hold this value. Peltier = A, Peltier(1) = B, Peltier(2) = C, Peltier(3) = D, Peltier(4) = E, The string should look like this A,B,C, D, E But in my code it's more like C D B A E. No logical order whatsoever?
Thanks for reading, hope you can help me !
Answer by TeHuster · Mar 23, 2017 at 10:22 AM
I think i fixed it by using:
using System.Linq;
and:
peltiers = GameObject.FindGameObjectsWithTag("Peltier").OrderBy( go => go.name ).ToArray();
Whooohooo!