"If boolean X started being true on this frame, do Y" Is this possible in C#?

I’ve been looking around all day for a solution to this issue. It seems really basic and that’s exactly why its so frustrating to not find a simple understandable answer.

All I want to do is in C#, make a piece of a script on an Update function run if a boolean started being true on the current frame.

By defintion, Update() is run once per frame;

Update()

if(nowTrue) {
// do something cool
// could set nowTrue off 
}

Since Update is run once per frame, you can cache values to compare them between frames.

public class FooChecker : MonoBehaviour {
	bool foo;
	bool lastFoo;
	
	void Start() {
		lastFoo = foo;
	}
	
	void Update() {
		//step 1: update values for this frame
		foo = Random.value < 0.5f;
		
		//step 2: check if values have changed
		if (foo != lastFoo) {
			if (foo) {
				Debug.Log("Foo just became true on frame " + Time.frameCount);
			} else {
				Debug.Log("Foo just became false " + Time.frameCount);
			}
		}
		
		//step 3: cache values for next frame
		lastFoo = foo;
	}
}