Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

click is not working on my button, what am i doing wrong ??

Discussion in 'Scripting' started by jessica1986, Sep 16, 2012.

  1. jessica1986

    jessica1986

    Joined:
    Feb 7, 2012
    Posts:
    621
    Debug.Log ("hey! you clicked on: " + customerSearchResult );
    This line is working perfectly fine in this code-

    Code (csharp):
    1.         void OnGUI()
    2. {
    3.         if(length!=null){
    4.        // GUILayout.BeginScrollView();
    5.        
    6.             scrollViewVector = GUILayout.BeginScrollView(scrollViewVector, GUILayout.Width(100), GUILayout.Height(100));
    7.             //GUILayout.Label(innerText);
    8.             for (int i=customerSearchResult.Length-1; i>=0; i--){
    9.                         if (GUILayout.Button(customerSearchResult[i], "label") ) {
    10.                         Debug.Log ("hey! you clicked on: " + customerSearchResult[i] );
    11.                 }
    12.                 }
    13.         GUILayout.EndScrollView();
    14.         }
    15. }

    whereas the same line is not working from the code below -

    Code (csharp):
    1.     void OnGUI()
    2. {
    3.                 if(length>0){
    4.             int forGuiArrangement = 1;
    5.                         scrollViewVector = GUI.BeginScrollView (new Rect (260, 50, 100, 100), scrollViewVector, new Rect (0, 0, 400, 400));      
    6.                         for (int i=customerSearchResult.Length-1; i>=0; i--){
    7.                 forGuiArrangement = forGuiArrangement+1;
    8.                         if (GUI.Button(new Rect(20, 10*forGuiArrangement, 150, 100), customerSearchResult[i], "label")) {
    9.                         Debug.Log ("hey! you clicked on: " + customerSearchResult[i] );
    10.                 }
    11.                 }
    12.         GUI.EndScrollView();
    13.                 }
    14.     }

    What is wrong that i am doing ??
     
  2. robin-theilade

    robin-theilade

    Joined:
    Jun 3, 2012
    Posts:
    119
    In this case you should use GUILayout for the button. That will make the code work and you don't have to think about the button positioning.
     
  3. jessica1986

    jessica1986

    Joined:
    Feb 7, 2012
    Posts:
    621
    but how to position its x and y ?? cn you guide me on it
     
  4. robin-theilade

    robin-theilade

    Joined:
    Jun 3, 2012
    Posts:
    119
    You've set the height for each of the buttons to 100 but you only increment the Y value by 10 each time so the buttons overlap. Try setting the height to 20 and increment by 20.

    Also try moving forGuiArrangement = forGuiArrangement + 1; so it comes after your GUI.Button call, that will remove the empty space at the top.