C
C#7d ago
ShowTime13

✅ 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.
9 Replies
Angius
Angius7d ago
There's no such thing as using all the elements of a collection all at once What you could do, maybe, is give the cube class a .ToString() overload and use string.Join() to join the string outputs of each cube But, pretty sure, it still uses a loop under the hood
ShowTime13
ShowTime13OP7d ago
Got it. Thank you. Do I mark the question as solved somehow?
Angius
Angius7d ago
$solved uh $close
MODiX
MODiX7d ago
If you have no further questions, please use /close to mark the forum thread as answered
Angius
Angius7d ago
Still uses a loop
ShowTime13
ShowTime13OP7d ago
So that's a fundamental sorta limitation? In the end of the day, processor runs things in series, so whatever I do, it'll run one after another, is that right? The only way to avoid this is to use parallel processors, like GPUs?
Angius
Angius7d ago
Well, CPUs have multiple cores nowadays, so you can do miltithreading on a CPU if you wanted to But there's no point doing that just to display some strings Other than that, yes, everything runs in series
ShowTime13
ShowTime13OP7d ago
No, it's more like I'm interested in the fundamentals.. Got it I guess that's it then. thanks for the help

Did you find this page helpful?