C
C#6d ago
ShowTime13

✅ Logically, is it possible to update the List only if there's a change in the list itself?

What I'm trying to do: I'm trying to get List of items found using a tag (GameObject.FindGameObjectsWithTag("Cube")), and to optimize, I want the list update to only happen if the quantity of gameobjects changed. But the problem I see is that, how would the cubesList check, if only check is by list update, and it is bound to not update if it's same as before. So, here is the code: public class Attraction_test : MonoBehaviour { int previousCount; List<GameObject> cubesList = new List<GameObject>(); // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { if (cubesList.Count != previousCount) { cubesList.AddRange(GameObject.FindGameObjectsWithTag("Cube")); previousCount = cubesList.Count; } print("cubesList Count "+cubesList.Count); foreach (GameObject cubeList in cubesList) { print("CubesList: " + cubeList.name); } } }
2 Replies
Jimmacle
Jimmacle6d ago
you need to be checking the count of gameobjects in whatever main collection FindGameObjectsWithTag searches, not your own list
ShowTime13
ShowTime13OP6d ago
I see. Thank you

Did you find this page helpful?