Home > OS >  Problem adding OnClick event to buttons in a ScrollView
Problem adding OnClick event to buttons in a ScrollView

Time:03-23

I have done this before without problems but now it does not work.

I am implementing FriendsList in PlayFab and is populating a ScrollView list with a prefab button. I add an OnClick event to each button but I cannot get it to trigger.

I have tested different methods but still can not get it to work.

I did test by adding a button to the bottom of the screen and outside the loops and that worked. I have tested to add the buttons to a List and then add the OnClick after I populated the scrollview.

Here is the code I use currently:

void DisplayFriends(List<FriendInfo> friendsCache)
{
    DestroyFriendsContainer();

    foreach (FriendInfo _friends in friendsCache)
    {
        GameObject _listing = Instantiate(friendsPrefab, friendsContainer);
        _listing.name = _friends.TitleDisplayName;

        var _button = _listing.GetComponent<Button>();
        _button.onClick.AddListener(SelectPlayerActivity);
        print(">> "   _button.name);

        FriendsListing _tempListing = _listing.GetComponent<FriendsListing>();
        Debug.Log(_tempListing.txt_PlayerName);
        Debug.Log(_friends.TitleDisplayName);
        _tempListing.txt_PlayerName.text = _friends.TitleDisplayName;
    }

    friendsCache.ForEach(f => Debug.Log(f.FriendPlayFabId));

}

void SelectPlayerActivity()
{
    print("Player Name: ");
}

Here is a picture of the actual scene:

enter image description here

CodePudding user response:

You can make it public the void and make the ButtonHandler in a gameObject and use the object

  • Related