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); }
}
}
3 replies
✅ Is there a way to use the whole array or list, instead of looping through it?
My goal is to find a lot of objects, then apply certain effect on them all instantaneously.
1. Let's say I'm creating GameObject[] cubes = GameObject.FindGameObjectsWithTag("Cube"); array. Now I have the array in memory which contains cubes[0] to cubes[500]. (or list, if that makes it any different).
2. As I'm asking ChatGPT, it tells me that you can't use all the variables from the array at once and I have to loop through them with for or foreach.
How can I create variables for each of the cube internally in the script, so that I can access them all separately?
Here's how I see it:
If all the cubes are stored in memory after I declare and assign the list, there must be a way to use the WHOLE list, right?
Say, could it be possible to, in the code, dynamically create variables based on the findings of the array or list?
Then what I wanna do is "Debug.Log("Found cubes: " + cubes);" and by "cubes" ( know it doesn't work this way, just trying to explain what I mean) I mean "Show me the Debug.Log of each cubes (being it cubes1, cubes2, cubes3) that you can find", meaning no foreach looping.
Please, help me, what am I missing?
I also think that, possibly running code like:
Debug.Log("Found cubes: " + cubes[0]);
Debug.Log("Found cubes: " + cubes[1]);
Debug.Log("Found cubes: " + cubes[2]);
Debug.Log("Found cubes: " + cubes[3]);
is the same as looping through them, since code still runs line by line. Is that so? That'd mean I don't have to avoid looping.
20 replies