Hello community I am having problems with creating a game where I have six objects with the same script where it has an array on it, the problem here is that each object has different array, so it only works if I use for to update all of the index of the array, is there a way to have each object working for each room? with one script?
Or is there could be a problem updating all of those array rooms created in each object? Six objects with an array of 6 rooms per one, total of 36 rooms of arrays.
It’s unclear what your intentions are. What kind of data is in these arrays?
//Create or reference the objects in a script.
GameObject[] gos = new GameObject[6];
//In Update iterate the array of GameObjects
void Update()
{
for(int i = 0; i < 6; i++)
{
//run code for gos[i]
}
}
You can design it either way, with an array in each script, or all 6 arrays in one master script, there are pros and cons to each way. If you need to change things from a master script anyway, maybe just have them all in the master script. But if they are better as independent arrays, then just have one on each object.
Sorry I had been unclear, those array types are int and double wanting to have each room working for only one gameobject, total of six objects but the main problem here is that I need to use all of those arrays in those objects. Using for loop works but using it as it’s index doesn’t work
So each gameObject is for a specific room and it has an array, but it also needs access to the arrays in the other gameObjects? If that’s the case, I’d put everything in the master script and you can send values to each object as needed. This way the data all comes from one main place. If the gameObjects also can manipulate the data, you’ll need to update the master data with any changes. Whether you keep local copies in each gameObject depends on what you need to do with it, but typically you should keep it in just one place if possible. Again, there are multiple ways to do this, I prefer one main script and the instances are fairly agnostic about the data.
You can look to 2D and jagged arrays for your main script data structure too.
Hello seejayjames thanks for the response. Sorry for my bad spelling gotta upload it again