Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Array problem - using array element value

Discussion in 'Scripting' started by tattyswad, Oct 21, 2013.

  1. tattyswad

    tattyswad

    Joined:
    Sep 22, 2010
    Posts:
    106
    Hello,

    I am creating a script that I can use to create a GUI using GUI Textures. The following script is almost where I want it but not quite. I've only just started using Arrays and I'm finding it quite confusing.

    Code (csharp):
    1. var buttonHeight : float;
    2. var buttonWidth : float;
    3. var buttons : GameObject[];
    4.  
    5. function Start () {
    6.  
    7.     transform.position = Vector3.zero;
    8.     guiTexture.pixelInset = Rect(Screen.width - buttonWidth,Screen.height - buttonHeight, buttonWidth, buttonHeight);
    9.    
    10.     for (var i : int = 0; i<buttons.Length; i++){
    11.         Debug.Log ("this is button No"+i);
    12.    
    13.     for (var myButtons : GameObject in buttons){
    14.     myButtons.guiTexture.pixelInset = Rect(Screen.width - buttonWidth, Screen.height - ((buttonHeight*i) + buttonHeight), buttonWidth, buttonHeight);
    15.     }
    16. }
    17.  
    18. }
    The idea with this is that I have one script for creating a toolbar of any size, rather than relating a script for each toolbar that I have to create. To make this script work I would create a number of GUI Textures, one would be the 'Header' if you like. This would be the parent and I would make the other buttons children of this. I would then put the script on the header object, specify the number of buttons and then 'theoretically' the script would space out the buttons below the header.

    I want to multiply the height of the button by the variable 'i' which should be the arrays element value (ie [0], [1], [2] etc)
    The problem is that the buttons vertical position is being multiplied by the highest value of 'i' rather than the value that relates to the specific array element value. The result being the header is in the correct position but the rest of the buttons are piled one on top of each other at the bottom of the toolbar. Can anyone suggest how to fix this?

    If I get this working it's really going to save me a lot of time!

    Thanks
     
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    You are using the i variable from your first for loop in your second for loop, but they are nested, because they are nested, the second loop goes through the entire set before the next iteration of the first loop. You aren't even using the first loop, except apparently to get the i counter so it can be eliminated, I think, not positive what you are doing there:

    Code (csharp):
    1.  
    2. for (var i : int = 0; i<buttons.Length; i++){
    3.  buttons[i].guiTexture.pixelInset = Rect(Screen.width - buttonWidth, Screen.height - ((buttonHeight*i) + buttonHeight), buttonWidth, buttonHeight);
    4. }
    5.  
    6.  
     
    Last edited: Oct 22, 2013
  3. tattyswad

    tattyswad

    Joined:
    Sep 22, 2010
    Posts:
    106
    Hi fire7side,

    That's sorted it,....I kind of knew I had to use the i variable from the first loop but wasn't sure how!

    Thanks for your help.