Update runs only once

Good day!

I experienced an unusual problem.After adding some left-right movement to an enemy(using velocity in FIxed Update),my Update function runs only once.For ex. if I add Debug.Log(“Hello”),it appears in console only in the beginning!Please tell me what is the problem.

public class FlyerEnemyMove : MonoBehaviour {

    public float movingSpeed;
    private int direction;
    public float maxDist;
    public float minDist;

    // Use this for initialization
    void Start ()
    {
        direction = -1;
        maxDist += transform.position.x;
        minDist -= transform.position.x;
    }


    void FixedUpdate()
    {
        movingLeftAndRight ();
    }

    void Update ()
    {
                Debug.Log("Hello");

    }

    void movingLeftAndRight()
        {
            switch (direction)
            {
            case -1:
            {
                // Moving Left
                if( transform.position.x > minDist)
                {
                    GetComponent <Rigidbody2D>().velocity = new Vector2(-movingSpeed,GetComponent<Rigidbody2D>().velocity.y);
                }
                else
                {
                    direction = 1;
                }
                break;
            }
            case 1:
            {
                //Moving Right
                if(transform.position.x < maxDist)
                {
                    GetComponent <Rigidbody2D>().velocity = new Vector2(movingSpeed,GetComponent<Rigidbody2D>().velocity.y);
                }
                else
                {
                    direction = -1;
                }
                break;
            }
               
            }
        }
}

Thank you very much!

Have you doubled checked that you don’t have “collapse” enabled in the console window?

1 Like

Good day sir!

Thank you very much!This was the problem.I really appreciate your help!

Have a nice day!

1 Like

You can use Collapse, just pay attention to the number on the right, which indicates how many times the Debug.Log line has been printed.

–Eric

1 Like