✅ 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
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 hoodGot it.
Thank you.
Do I mark the question as solved somehow?
$solved
uh
$close
If you have no further questions, please use /close to mark the forum thread as answered
Here's what string.Join() does, btw: https://github.com/dotnet/runtime/blob/1d1bf92fcf43aa6981804dc53c5174445069c9e4/src/libraries/System.Private.CoreLib/src/System/String.Manipulation.cs#L775-L783
GitHub
runtime/src/libraries/System.Private.CoreLib/src/System/String.Mani...
.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps. - dotnet/runtime
Still uses a loop
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?
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
No, it's more like I'm interested in the fundamentals..
Got it
I guess that's it then.
thanks for the help