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

Type IPointerClickHandler expected Button received. i:0. Argument is out of range.

Discussion in 'UGUI & TextMesh Pro' started by Diablo404, Aug 23, 2014.

  1. Diablo404

    Diablo404

    Joined:
    Mar 15, 2013
    Posts:
    136
    Hey,

    Following my previous thread ( http://forum.unity3d.com/threads/button-action-with-arguments.263613/ )

    I tried to format things a little bit like so :

    Code (JavaScript):
    1. var btnNames : List.<String> = new List.<String>() ;
    2.  
    3. function Awake ()
    4. {
    5.  
    6.     btnNames.Add ( "Button1" );
    7.     btnNames.Add ( "Button2" );
    8.     btnNames.Add ( "Button3" );
    9.  
    10.     for( var k : int = 0; k < btnNames.Count ; k++ )
    11.     {
    12.         if( GameObject.Find(btnNames[k]) ) GameObject.Find(btnNames[k]).GetComponent(UI.Button).onClick.AddListener( function() { OnButtonClick(btnNames[k]); } );
    13.     }
    14.  
    15. }
    16.  
    17. function OnButtonClick ( _which : String )
    18. {
    19.     switch ( _which )
    20.     {
    21.         case "Button1":
    22.             print("Btn1");
    23.         break;
    24.  
    25.         case "Button2":
    26.             print("Btn2");
    27.         break;
    28.  
    29.         case "Button3":
    30.             print("Btn3");
    31.         break;
    32. }
    33. }

    But unfortenetely, when clicking a button, I got this error:

    Type IPointerClickHandler expected Button received. i:0. Argument is out of range.
    Parameter name: index
    UnityEngine.EventSystems.EventSystem:Update()

    Am I doing something wrong?

    Thans in advance
     
    Last edited: Aug 23, 2014
  2. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,181
    There is a typo here:
    for( vark :int=0; k < btnNames.Count; k++)
    vark instead of var k, maybe the script isn't compiling properly?
     
  3. Diablo404

    Diablo404

    Joined:
    Mar 15, 2013
    Posts:
    136
    Sorry about that, it's due to copy/paste into this forum, there was lot of things to re-arrange and I forgot that one.
    The typo isn't in my script and it's compiling very well!
     
  4. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,181
    So the error your seeing is not a clear error. Something is wrong when we issue the callback, but it's not dropping a nice stacktrace. I've improved the error for the next release so you'll be able to tell more clearly what the issue is. From just that code I can't figure out exactly what the issue might be.
     
  5. Senshi

    Senshi

    Joined:
    Oct 3, 2010
    Posts:
    556
    It appears UnityEvent.AddListener() takes a UnityAction as its parameter; perhaps that's where the error is? I'm using C#, so I can't immediately test this, but perhaps you might also get away with:

    Code (csharp):
    1. [...].onClick.AddListener( function(buttonParam) { OnButtonClick(btnNames[k]); } );
    If it helps, I got nearly the exact same error message when I was feeding ExecuteEvents.Execute() the wrong type of EventData; perhaps you have some faulty code there instead?
     
  6. Diablo404

    Diablo404

    Joined:
    Mar 15, 2013
    Posts:
    136
    Well, there is nothing more on the script nor in the project. But okay, I'll wait until the error become clearer! Thanks!

    @Senshi : I don't know, but what I can say is that if I place a string directly ( OnButtonClick("test") ) instead of the value inside an array, it's working just fine.
     
  7. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,181
    Can you zip the project and attach it here, i'm about to jump on a flight and can take a look while flying if you are quick :)
     
  8. Diablo404

    Diablo404

    Joined:
    Mar 15, 2013
    Posts:
    136
    Well, wasn't quick enough. If by any case you want / have time to have a look here it is.

    As a workaround I used a similar script attached to every button GO and then check the name to manage actions.
     

    Attached Files:

  9. gegebel

    gegebel

    Joined:
    Jan 29, 2014
    Posts:
    469
    I have the same issue using List too. I'm using the same code in 4.5 and it works there. Seems like a problem with List.
     
  10. Diablo404

    Diablo404

    Joined:
    Mar 15, 2013
    Posts:
    136
    Actually I've tried with built in arrays and doesn't change a thing
     
  11. ippdev

    ippdev

    Joined:
    Feb 7, 2010
    Posts:
    3,789

    Good stuff. I had alot of those in a complex GUI based on array lengths etc. The call was maddening as it told nothing about where it was or what scripts were associated with it etc. I has to go back in and put print() after each loop to see which loops were out of range when instantiating UI objects.

    For the others..once I took care of out of range exceptions it worked and was not due to the UI.
     
  12. Tim-C

    Tim-C

    Unity Technologies

    Joined:
    Feb 6, 2010
    Posts:
    2,181
    rakkarage likes this.
  13. Diablo404

    Diablo404

    Joined:
    Mar 15, 2013
    Posts:
    136
    Well well well, first, I have to tell that I really didn't understood why the making of specificaly 3 loops would have crash the thing but I tried anyway. I tried with 10 but same bug occured.

    Then, I know just a few about closure, but I checked your link. The thing that I can relate is that if inside my loop, instead of using the array value at index ( array[ i ] ) , I use a temporary variable that I fill with this value, and THEN I pass this value to the function, at this time I only get the last occurence of the loop when print().
     
  14. gegebel

    gegebel

    Joined:
    Jan 29, 2014
    Posts:
    469
    I managed to go around the problem by storing my vars in a GameObject which is not part of the Canvas/UI. Apparently, Canvas elements like panels, buttons and so on are not considered a GameObject.
     
  15. bwheatley

    bwheatley

    Joined:
    Jun 23, 2012
    Posts:
    45
    gegebel could you please give an example? I'm running into this issue as well.
     
  16. gegebel

    gegebel

    Joined:
    Jan 29, 2014
    Posts:
    469
    I first used several scripts and tried to access them via script, but when the scripts were attached to new UI stuff, I couldn't get any reference to them, always got "out of range" or other strange errors.
    Now I created an empty GameObject with a script attached to it. Whenever I need a button to have a reference I can adjust it by adding a reference to my empty GameObject in the "On Click" part of the button in the Inspector.
    Another possibility would be to use the "singleton" method by using public static variables instead of just public. Note that static vars are only available once though but can be referenced without using GameObject.Find (Which is a bad programming habit anyway).
     
    JohnParask likes this.
  17. bwheatley

    bwheatley

    Joined:
    Jun 23, 2012
    Posts:
    45
    Thank you for sharing Gegebel!
     
  18. JohnParask

    JohnParask

    Joined:
    Jul 7, 2014
    Posts:
    323
    I have the same problems, can you help me out, describe how you add the reference

    buttonClone.GetComponent<Button>().onClick.AddListener(delegate {myVoid(mystring);});

    and i get weird errors. I'm getting crazy from errors with no sense :p
     
  19. Andy-Block

    Andy-Block

    Joined:
    Aug 10, 2012
    Posts:
    10
    Wrapping the click handler in a try/catch and logging the resulting exception quickly found my problem...
    Code (CSharp):
    1.     public void OnPressed()
    2.     {
    3.         try {
    4.             thing.DoYourJob();
    5.         } catch (System.Exception e) {
    6.             Debug.LogError(string.Format("OnPressed exception: {0}", e));
    7.         }
    8.     }
     
    mark_madatom likes this.