- Home /
How to deal with for loop and array?
Hi! I'm working on an graphical interface to control Ableton Live using OSC (Unity OSC) to trigger clips in songs. My problem does not have to do with that at all (my osc stuff works great), but with such a seemingly simple thing as for loops and an array. What I want do achieve is that I have an array with let's say 5 elements (tracks in ableton live). Each element in the array corresponds to the number of audioclips in a track. (int[] liveTrack = {3,2,4,5,3};)
I want to go through the array and instantiate prefabs in a grid. The prefab is just a cube with a texture, with a 3d text child. Ideally the first clip in first track is in upper left and the next one beneath that, next track to the right of the previous track and so on... Maybe a small offset to get some space between. This is replicating the layout in Ableton Live. Best thing would get everything centered on x axis. And just high enough on Y for the track with most clips to fit above Y0. Also, I want it to be dynamic so i can use another array with more elements if I need.
Each clip should also get variables assigned to them. One for the track clip belongs to and clip number (These are for creating the OSC message to trigger them later).
At one point i got the prefabs in the right place, but the variables that were assigned were really funky. The first object got like 4 and 2. Strange stuff.
This is what I have right now. I can't even see straight right now. :) It's driving me crazy!
Is this the best way to do it?
(BTW all this is being controlled with the Razer Hydra controller, and later using the Oculus Rift HMD. Virtual OSC controller!)
using UnityEngine;
using System;
using System.Collections;
public class controlsCreator : MonoBehaviour {
public GameObject padPrefab;
private int[] liveTrack = {3,2,4,5,3};
// Use this for initialization
void Start ()
{
for (int x = 0; 0 < liveTrack.Length; x++)
{
Debug.Log("XVAL: " + x);
for (int y = 0; 0 < liveTrack[x]; y++)
{
Vector3 pos = new Vector3(x, y, 0);
Instantiate(padPrefab, pos, Quaternion.identity);
padControl padScript = padPrefab.GetComponent<padControl>();
padScript.padOSCAddress = "/live/play/clip";
padScript.padTargetTrack = x;
padScript.padTargetClip = y;
}
}
}
}
Not really sure what you are trying to do, but the correct way to iterate over the array is: for (int x = 0; x < liveTrack.Length; x++)
Remember: for (initializer; condition; iterator)
Your condition was (0 < liveTrack.Length) which will never be false, which would make it run infinitely
Your answer
Follow this Question
Related Questions
Prefabs instantiated from an array are keeping their public int value 1 Answer
OverlapSphere for parallel arrays 1 Answer
Instantiating an array of objects - how can I instantiate a certain prefab only once? 1 Answer
Spawning a prefab at another object's location 3 Answers
Issue Instantiating prefab in C# 0 Answers