Function not working

So i have a problem here, and i tried for hours to find the issue. So i have this enemy, (in 2D), and it has a circle trigger collider. When the player enters it, it is supposed to stop walking towards the player, wait a little, then do an attack animation. It supposed to continue this animation until the player exits the trigger radius.

So the problem is, that it is walking towards the player, and once the player enters the 2D collider, the enemy stops, and just freezes. It still changes it rotation to look at the player obviously, but its just… stuck.

Any ways here is my code, hope you can help me;

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Enemy : MonoBehaviour {

float speed = 1f;
float life = 10f;
public SpriteRenderer sprite;
public Sprite walking1;
public Sprite walking2;
public Sprite attacking1;
public Sprite attacking2;
public Sprite idle;
public Sprite death;
public GameObject gotoplaypos;
bool walking = true;
float timer;
bool attacking = false;
bool wait = false;

// Use this for initialization
void Start()
{
    enabled = false;
}

void Update()
{
    timer += Time.deltaTime;
}

// Update is called once per frame
void FixedUpdate () {
    if (Vector2.Distance(gotoplaypos.transform.position, transform.position) < 25f)
    {
        Vector3 dir = gotoplaypos.transform.position - transform.position;
        float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
        transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);

        if (walking == true)
        {
            transform.position += transform.right * speed * 1.15f * Time.deltaTime;

            if(timer < 0.4f)
            {
                sprite.sprite = walking1;
            } else
            {
                sprite.sprite = walking2;
                if(timer > 0.8f)
                {
                    timer = 0;
                }
            }
        } 
    }
}

private void OnTriggerEnter2D(Collider2D other)
{
    if(other.tag == "player") {
        walking = false;
        attacking = true;
    }
}

private void OnTriggerStay2D(Collider2D collision)
{
    if (collision.tag == "player")
    {
        if (wait == false)
        {
            attack();
        }
    }
}

private void OnTriggerExit2D(Collider2D collision)
{
    if (collision.tag == "player")
    {
        walking = true;
        attacking = false;
    }
}

private void attack()
{
    wait = true;
    if (timer > 0.1f && 0.3f > timer)
    {
        sprite.sprite = attacking1;
    } else
    {
        if (timer > 0.3f && 0.5f > timer)
        {
            sprite.sprite = attacking2;
        } else
        {
            if(timer > 0.5f)
            {
                timer = 0f;
                wait = false;
            }
        }
    }
}

}

void Start()
{
enabled = false;
}

Isn’t this the problem? If you set Behaviour.Enabled to false I think it should stop doing the Update function. This script is based on the timer, which is updated in Update. So your timer is always 0.

The reason i stop set the script to unabled is because i have so many enemies, and without disabling some when they ate not close enough to the player, the game will lag.
I have another script, which tells the script you are currently looking at, to “active” once the gameobject is close enough. With other words, the update function IS active, i can see that in the inspector too, but it’s still not working… any other solutions?

using UnityEngine;

public class Enemy : MonoBehaviour
{
private float speed = 1f;
private float life = 10f;

public SpriteRenderer sprite;
public Sprite walking1;
public Sprite walking2;
public Sprite attacking1;
public Sprite attacking2;
public Sprite idle;
public Sprite death;

private float timer = 0;

// we will reset timer on every state change
// just one one bool is enough
private bool attacking = false;

// Why update function? It will work better if
// you will make bigger collider (25 units length)
// and make it trigger. This way you don't check
// if player is in range every frame even if your
// enemy is on the other side of the map.
// You only need to check if player is in range of attack
// when he is inside trigger. I assumed 0.5f. Adjust it. 

private void OnTriggerStay2D(Collider2D collision)
{
    if (collision.gameObject.tag != "player")
        return;
    
    Vector2 obj_to_player = collision.gameObject.transform.position - transform.position;
    timer += Time.deltaTime;

    // you should go for squared magnitude for performace reasons
    if (obj_to_player.sqrMagnitude < 0.5f * 0.5f)
    {
        if (!attacking)
            timer = 0;

        Attack();
        return;
    }

    if (attacking)
        timer = 0;

    float angle = Mathf.Atan2(obj_to_player.y, obj_to_player.x) * Mathf.Rad2Deg;
    transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
    transform.position += transform.right * speed * 1.15f * Time.deltaTime;

    if (timer > 0.4f)
    {
        sprite.sprite = walking2;
        if (timer > 0.8f)
            timer = 0;
        return;
    }
    sprite.sprite = walking1;
}

private void Attack()
{
    if (timer > 0.1f && timer < 0.3f)
        sprite.sprite = attacking1;
    else if (timer > 0.3f && timer < 0.5f)
        sprite.sprite = attacking2;
    else if (timer > 0.5f)
        timer = 0f;
}

}

Try this. I can’t see how it works, so it can have some errors. But it shows the idea I think. Maybe you had problems with collision.tag. I’m not sure how it works so I switched it to collision.gameObject.tag.

if(bool _bool)
something;

is the same as

if(bool _bool)
{
something;
}

As long as “something” is just one line. If you don’t like it add {}. If you use if(){return} your code will be easier to read.