Specific question + best practices for bools that are true for one frame only and need to be detected by other scripts

Hi, would appreciate some insight on this issue if anyone has any:

In my main game I have a selection system setup so the player can click different interactable objects and use them. I have two bools set up in the Selection script, isSelected which remains true for every frame that the object is selected for, until it’s deselected, and selectStart which should just register true for the first frame it was selected, then change to false on the next frame. Both bools are public because other scripts need to detect them to do their thing.

The way I had it working so that selectStart is only true for the first frame is to detect if it’s true in LateUpdate and then set it to false. This was working fine and was detectable from other scripts as well:

In Selectable script:

 public void SelectMe()
    {
        isSelected = true;
        selectionStart = true;
    }

   void LateUpdate()
    {
        if (selectionStart)
                  selectionStart = false; //so that wasSelected is only true for the first frame where it was selected

    }

In Secondary Script:

void Update()
{
     if (selectable.selectionStart)
     {
           print(gameObject.name + "'s selectionStart triggered"); 
           //do other stuff like play particle FX... 
     }
}  

I then started a new project for Ludum Dare this weekend and borrowed a bunch of things from the original game, including the Selection script (with isSelected and selectStart) and another script that detects for these variables. Except in the new game, the second script doesn’t detect selectStart successfully. The scripts are exactly the same, and no changes were made to script execution order. I then noticed that if I change the second script so it looks for selectable.selectionStart in LATE update instead of update, it does work. So I’m not sure what causes this exactly.

Also the followup question would be, is this a hackey way of doing things? What is the best practice way of having a “trigger type” bool, true for only one frame, be detectable in another script?

Thanks for any help!

Excuse me if the following suggestion does not fit your needs, I may not have all the information.

If I understand correctly, in the end, you want the secondary script to be warned when SelectMe is called right? You don’t really needs the booleans, do you?

I would use events in your case and completely put aside booleans .

// Selectable script

public UnityEngine.Events.UnityEvent OnSelected;

 public void SelectMe()
 {
     if( OnSelected != null )
         OnSelected.Invoke();
 }

// Secondary script

private void Start()
{
    selectable.OnSelected.AddListener( OnSelectableSelected ) ;
}

private void OnSelectableSelected ()
{
    print(gameObject.name + "'s selectionStart triggered"); 
}