Ok so I have a list of GameObjects which I sort based on a distance calculation from another script. I originally had this in the Start() function but I had to move it to Update() because I wanted the list to sort properly based on the distance from the player.
Although as you might see after looking at my code, it is very processor intensive and I am looking for a way to make it much more efficient or is there some other way around this.
// Update is called once per frame
void Update ()
{
selectableObjects.Clear ();
selectableStrings.Clear ();
GameObject[] gos = GameObject.FindGameObjectsWithTag ("Selectable");
System.Array.Sort (gos, CompareObNames);
foreach (GameObject go in gos) {
print (go.GetComponent<SelectableObject> ().selectedDistance);
if (go.GetComponent<SelectableObject> () == null) {
Debug.LogError ("Each selectable object must have the selectableObject.cs script attached to it!");
}
selectableObjects.Add (go);
selectableStrings.Add (go.GetComponent<SelectableObject> ().selectedName);
}
}
int CompareObNames (GameObject x, GameObject y)
{
return x.GetComponent<SelectableObject> ().selectedDistance.CompareTo (y.GetComponent<SelectableObject> ().selectedDistance);
}
Your code is very processor intensive because you’re callng FindGameObjectsWithTag in every frame update.
Declare GameObject[ ] gos as a member variable, and assign the gameobjects to it in your Start() method.
You can then do your distance calculations in Update()
For better performance, put your distance calculator in a co-routine that only fires every 0.1 seconds or whatever value you deem is accurate enough for you.
If the list of things changes during run time and you need to update it, I’d suggest changing the structure around so that the script registers/removes itself from the list in OnEnable and OnDisable as opposed to using a master script to search for all of them. The performance will be much better, it’ll be properly dynamic, and you can just sort the list when appropriate as opposed to finding all the objects, putting them in a list, and sorting the list each frame (finding them is, by far, the most expensive step there).
@Meldown Just moving the object finder helped speed it up alot and that co-routine really helps free up a little more time, thanks.
@KyleStaves I am not sure how I would do that, atm I just have it setup to find all the objects on a function call and whenever a new object is created it will fire that function to update the selectable object list. If doing the way you have described is much more efficient then perhaps I can look into it further. Could you point me in the right direction please.
EDIT: Sorry for double post, it timed out.
Is this used for runtime or Editor?
Looks like an editor script, so you can rebuild your gos inside an OnHierarchyChange()
This is all runtime
EDIT: Although looking at the OnHierarchyChange() function may be an answer to what I need, unless that object is not an object with the selectableObject script attached.