Delete the method of a button

Hi,

I am developing a gallery that show video and photo.

I am using an array of six buttons that will store the reference of a photo or video and when you press one, it will reproduce the content by going to the respective video or photo method.

When I add a video and I reproduce it works very well, the same when I add a photo and it still works. But when I want to return to the previous button to play the video it shows me an error because I try to play the video with the photo method, the same happens when I reverse things and I want to reproduce a photo.

This is my main code that is responsible for assigning the methods to the buttons:

   public GameObject _photoSphere;
    public GameObject _videoSphere;

    public TextMeshProUGUI[] _buttonFileName;

    //Botones para la interfaz, agregar, borrar y plays
    public Button[] _addFile;
    public Button[] _deleteFile;
    public Button[] _playFile;

    //Iconos para los archivos
    public SVGImage[] _photoIcon;
    public SVGImage[] _videoIcon;

    #region Metodos para inicializacion

    private void Start()
    {
        AddFileListenerOnClick();
        DeleteFileListenerOnClick();
        PlayFilesOnClick();        
    }

    #endregion

    #region Metodos para botones

    //Asiganamos metodo para abrir el explorador de archivo al Metodo OnClick _addFile basandonos en el index seleccionado
    public void AddFileListenerOnClick()
    {
        for ( int i = 0; i < _addFile.Length; i++ )
        {
            int tempInt = i + 0;
            _addFile*.onClick.AddListener( () => _vRMEDExplorerScript.OpenFileBrowser( tempInt ) );*

}
}

public void DeleteFileListenerOnClick()
{
for (int i = 0; i < _addFile.Length; i++)
{
int tempInt = i + 0;
//_deleteFile*.onClick.AddListener(() => galleryControlsRPC.DeleteFileName(tempInt));
_
}*

}

//Asiganamos metodo para reproducir archivo al Metodo OnClick de PlayFliesOnClick basandonos en el index seleccionado
public void PlayFilesOnClick()
{
for ( int i = 0; i < _playFile.Length; i++ )
{
int tempInt = i + 1;
playFile*.onClick.AddListener( () => PlayFile( tempInt ) );*
}
}_

//Metodo que manda la instruccion de reproducir
public void PlayFile( int playIndex )
{
var video = Array.FindIndex( _loadVideoFileScript._videoFiles, row => row.Contains( “.mp4” ) );
var photo = Array.FindIndex( _loadPhotoFileScript._photoFiles, row => row.Contains( “.jpg” ) );

if ( _galleryControlsRPC.isVideo == true )
{
for ( int i = 0; i < _loadVideoFileScript._videoFiles.Length; i++ )
{
if ( video == i )
{
VideoReady( playIndex );
PlayFilesOnClick();
_playFile[playIndex].onClick.RemoveListener( () => PlayFile( playIndex ) );
}
}
}
if ( _galleryControlsRPC.isPhoto == true )
{
for ( int i = 0; i < _loadPhotoFileScript._photoFiles.Length; i++ )
{
if ( photo == i )
{
PhotoReady( playIndex );
_playFile[playIndex].onClick.RemoveListener( () => PlayFile( playIndex ) );
}
}
}
}

private void VideoReady( int videoReadyIndex )
{
_galleryControlsRPC._filePlayIndex = videoReadyIndex;
_galleryControlsRPC.SendRpcSetPlayVideo( _galleryControlsRPC._filePlayIndex );

_photoSphere.SetActive(false);
_videoSphere.SetActive(true);
}

private void PhotoReady(int photoReadyIndex)
{
_galleryControlsRPC._filePlayIndex = photoReadyIndex;
_galleryControlsRPC.SendRpcPlayPhoto( _galleryControlsRPC._filePlayIndex );
_ClassPlayer._VRMEDPlayer.CloseVideo();

_photoSphere.SetActive( true );
_videoSphere.SetActive( false );
}

//Este metodo se encarga de buscar en la lista principal los archivos con extencion
//.jpg y mp4 para almacenarlos en sus respectivas listas
public void AddFileToPlay()
{
if ( _loadVideoFileScript._videoFiles[_galleryControlsRPC._fileExplorerIndex].Contains( “.mp4” ) )
{
for ( int i = 0; i < _loadVideoFileScript._videoFiles.Length; i++ )
{
_videoIcon[_galleryControlsRPC._fileExplorerIndex].enabled = true;
_photoIcon[_galleryControlsRPC._fileExplorerIndex].enabled = false;
}
}

else if ( _loadPhotoFileScript._photoFiles[_galleryControlsRPC._fileExplorerIndex].Contains( “.jpg” ) )
{
for ( int i = 0; i < _loadPhotoFileScript._photoFiles.Length; i++ )
{
_videoIcon[_galleryControlsRPC._fileExplorerIndex].enabled = false;
_photoIcon[_galleryControlsRPC._fileExplorerIndex].enabled = true;
}
}
//_result = _photoFiles.ToList().Concat(_videoFiles.ToList()).ToArray();
}
here a screenshot of the gallery:
[136876-2.jpg|136876]
here the error message:
[136877-unity-201743f1-personal-64bit-360-vr-playerunity-v.jpg*|136877]*

*
*

Because () => PlayFile( playIndex ) creates an anonymous function, you can’t use this syntax to remove the listener. You are basically removing a freshly new created function.

When adding the listeners to your buttons, you have to store a reference to them somehow. An array / list will be enough.

// Pseudo-code

// Must be same length as _playFile
private UnityEngine.Events.UnityAction[] _listeners;

// ....

// Add
for( ... )
{
    UnityEngine.Events.UnityAction listener = () => PlayFile( playIndex );
    _listeners[playIndex] = listener ;
    button.onClick.AddEventListener( listener );
}

// Remove
for( ... )
{
    UnityEngine.Events.UnityAction listener = listener[playIndex];
    button.onClick.RemoveEventListener( listener );
}

Another way is to call RemoveAllListeners instead, but as indicated, it removes all the listeners added at runtime.