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. Dismiss Notice

Array in OnGUI

Discussion in 'Scripting' started by itech, Sep 16, 2010.

  1. itech

    itech

    Joined:
    Jul 28, 2010
    Posts:
    139
    Hi
    I need a help, I wish to create a list of GUILabels on OnGUI.
    I create list from Array , this is a script :

    Code (csharp):
    1. function OnGUI () {
    2.     // An absolute-positioned example: We make a scrollview that has a really large client
    3.     // rect and put it in a small rect on the screen.
    4.  
    5.    
    6.         scrollPosition = GUI.BeginScrollView (Rect (20,10,440,210),
    7.         scrollPosition, Rect (0, 0, 400, 800),false,true);
    8.    
    9.    
    10.    
    11.    
    12.          
    13.                      
    14.         for (var value : String in arr)
    15.        {
    16.             GUI.Label (Rect (80, 20 + yOff, textureToDisplay.width, textureToDisplay.height),textureToDisplay);
    17.             GUI.Label (Rect (160, 20 + yOff + 20, textureToDisplay.width, textureToDisplay.height), value,customGuiStyle);
    18.            
    19.            
    20.            
    21.             if (GUI.changed) {
    22.          
    23.                 GUI.changed = false;   // reset the value, so we can detect the next one
    24.                 yOff=20;
    25.             }else {
    26.                
    27.                 yOff+=20;  
    28.                
    29.             }
    30.        }
    31.  .
    32.     GUI.EndScrollView ();
    33.    
    34.    
    35. }
     
  2. laurie

    laurie

    Joined:
    Aug 31, 2009
    Posts:
    638
    Yes? What do you need help with? Are you getting errors, or unexpected behaviour? Or is there something more you want the script to do?
     
  3. mysticvalleycsa

    mysticvalleycsa

    Joined:
    Sep 22, 2020
    Posts:
    14
    Found this because I am trying to do the same, and OnGUI seems to be offended by my use of a for loop? I might be having an airgap but I can't reckon how to perform an action for each item in an array? It says "can only be called in OnGUI". This is so infuriating because it's such a simple thing to perform in Grasshopper Rhino. So, absurdly, fundamental. I need to display the mesh face indices, so I can write them tf down, so I can assign actions according to create an actually useful collisionFlag enumerator. For legibility, I center them on the face, horizontal to view. I just can't believe that through all this parsing information, mostly offered by volunteers, that Unity expects me to fork over 30% of profit when this is all said and done. At least I'm learning to code, which I might just take over to Godot. If I figure this out I'll reply with my code.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,713
    Sorry to hear you're having a hard time.

    One of the ways to improve that experience is to post a clear question (possibly with relevant code) in your own new fresh post, rather than hijacking an 11-year-old post that shares perhaps nothing in common with your problem.

    We know arrays can be handled in OnGUI because ... well, I do it all the time. Look at any one of my games.

    If you would like to make a fresh post of your own to restate your question clearly with a small snippet of relevant code, here is how to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    When you post code, please use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/
     
  5. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,524
    Like Kurt said, if you have a concrete issue, you should create your own post. I once created this IMGUI crash course on UnityAnswers which might be useful. I don't really see what could possibly be the issue with a loop inside OnGUI. As Kurt said, we use it all the time. I created countless editor tools that uses loops, like this sceneview camera preset script.
     
  6. mysticvalleycsa

    mysticvalleycsa

    Joined:
    Sep 22, 2020
    Posts:
    14
    Thank you for responding, and so quickly. Yes, I had accidentally commented out a curly bracket. I still have a problem which does still seem significant re: face vertex stability & calls. PS: Hijack is a strong word for picking up a hanging thread. I'll init a new thread.