Help with y-position

I’m a newbie here, but this is my code to switch gravity at certain y positions to make a sprite bob up and down, only problem is that the gravity keeps changing all the time, not only when it reaches the thresholds.

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

public class SetGravity : MonoBehaviour {

private Rigidbody2D rb2d;
private RectTransform rect;

private void Start()
{
rb2d = GetComponent ();
rect = GetComponent ();
}

void FixedUpdate()
{
float y = rect.position.y;

if (y >= 105 || y <= 75)
{
rb2d.gravityScale = -rb2d.gravityScale;
}

}
}

Any help would be appreciated.

Is the sprite a gui item? Just wondering why you are using recttransform and not just a transform.

y = 75.5 (-g => move -1)
y = 74.5 (+g => move +1)
y = 75.5 (-g => move -1)
y = 74.5 (+g => move +1)
y = 75.5 (-g => move -1)
y = 74.5 (+g => move +1)
y = 75.5 (-g => move -1)
y = 74.5 (+g => move +1)
y = 75.5 (-g => move -1)
y = 74.5 (+g => move +1)

I think you’re looking for “if > value and I’m falling up, or < value and I’m falling down” to keep things “in the middle”?

the sprite is an image created in the UI as an image on the canvas and yes, I’m trying to make it where when the Rect Transform Pos Y = 75, move up, When it reaches 105, move down. I thought it would be an easy way to bob my logo up and down on my startup scene.