Destroying a gameobject is not working. I tried triggering the colliders but an error comes out.

using UnityEngine;
using System.Collections;

public class EnemyController : MonoBehaviour {

//In the editor select if you want the enemy to move vertical or horizontal. Also set the distance to move.
public bool moveVertical = false;
public bool moveHorizontal = true;
public float distanceToMove = 3.0f;
public GameObject other;

private Vector2 initPosition;

void Start()
{
    initPosition = transform.position;
}

void Update()
{
    //Moves enemy horizontally
    if (moveHorizontal == true && moveVertical == false)
    {
        transform.position = new Vector3(Mathf.PingPong(Time.time, distanceToMove) + initPosition.x, transform.position.y, transform.position.z);
    }

    //Moves enemy vertically
    if (moveHorizontal == false && moveVertical == true)
    {
        transform.position = new Vector3(transform.position.x, Mathf.PingPong(Time.time, distanceToMove) + initPosition.y, transform.position.z);
    }

I got an error on this code in bold:
void OnTriggerEnter2D(Collider other) {

if (other.gameObject.tag == “Player”)
{
return;
}
Destroy(other.gameObject);
Destroy(gameObject);
}
}
}

I guess you are getting the “Object is destroyed, but you are still trying to access it” error message.

The reason is the execution order of Unity. This is the order that Unity callbacks are executed per frame. It can be found here: Unity - Manual: Order of execution for event functions

As you can see the Physics update is done before the Update() call. This means that during the physics update the two objects are destroyed, but when Unity executes the Update() later in the frame the objects are not yet removed, but marked as begin destroyed. This is not allowed.

In order to solve this problem, just do if (gameObject == null) return; at the beginning of your Update(). This should prevent the last Update() call. After the frame the objects are removed and no further Update() call is executed.