Help Please! (First Post)

My code keeps having problems. With the following code, an object is supposed to move up and down. I have if statements to switch the direction. The problem is that it won’t stop going up. Please help, thanks!


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

public class pickupScript : MonoBehaviour {
  
    private int waitTime = 0;
    public int speed = 1;

    private Vector3 pickupPos;
    private float yPos;

    private bool goingUp;

    private Vector3 newSpot;

    void Update () {

        pickupPos = gameObject.transform.position;
        Debug.Log(pickupPos);

        yPos = pickupPos.y;
      

        if (yPos == 5f) {

            goingUp = false;
            Debug.Log("yPos is 5");


        }

        if (yPos == 2f) {

            goingUp = true;
            Debug.Log("yPos is 1");

        }






        if (goingUp) {
            pickupPos.y += 1 * Time.deltaTime;
            gameObject.transform.position = pickupPos;
        }

        if (!goingUp) {

            pickupPos.y -= 1 * Time.deltaTime;
            gameObject.transform.position = pickupPos;

        }


    }
}
[code]

Try to use >= and <= for your comparisons, as it’s highly unlikely that you will ever land exactly on the numbers there.

Thank you!