Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by wehard · Jun 20, 2013 at 11:22 AM · c#instantiateprefabarrayfor

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;
             }
         }
     }
 }
 


Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image OmarAlhaddad · Jun 20, 2013 at 03:55 PM 0
Share

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

avatar image KiraSensei · Jun 20, 2013 at 04:10 PM 0
Share

Same for the "y" loop

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges