How to set button work order in VR model?

I made a basic CNC machine operating model in unity software.
In this model, I have made two pressable VR button and both of those buttons have different scripts added.
In my model, I want to do that
When I press the 1st button then only the second button should work, otherwise it should not.

How to do solve this problem.
Please help

To achieve this functionality, you can use a Boolean variable that will act as a toggle for the second button’s functionality. Here are the steps you can follow:

Create a Boolean variable called “isFirstButtonPressed” and initialize it to false.

In the script attached to the first button, set the “isFirstButtonPressed” variable to true when the button is pressed.

In the script attached to the second button, add a condition to check whether the “isFirstButtonPressed” variable is true or false.

If the “isFirstButtonPressed” variable is true, then the second button’s functionality should be executed. If the variable is false, then the second button should do nothing.

Reset the “isFirstButtonPressed” variable to false after the second button’s functionality is executed.

Here’s a sample code snippet to help you understand the implementation:

csharp
Copy code
public class FirstButtonScript : MonoBehaviour {

public bool isFirstButtonPressed = false;

public void OnButtonPress() {
    isFirstButtonPressed = true;
}

}

public class SecondButtonScript : MonoBehaviour {

public FirstButtonScript firstButtonScript;

public void OnButtonPress() {
    if (firstButtonScript.isFirstButtonPressed) {
        // execute the second button's functionality
        firstButtonScript.isFirstButtonPressed = false;
    }
}

}
In this code snippet, the “FirstButtonScript” script is attached to the first button, and the “SecondButtonScript” script is attached to the second button. The “FirstButtonScript” script contains the Boolean variable “isFirstButtonPressed,” which is set to true when the first button is pressed. The “SecondButtonScript” script has a reference to the “FirstButtonScript” script and checks whether the “isFirstButtonPressed” variable is true or false before executing the second button’s functionality.

I hope this helps you achieve the desired functionality in your CNC machine operating model in Unity.

You have to setup the button interact-able state. You didn’t supply any code to give context to your setup, but you have to do something similar to this:

using System;
using UnityEngine;

public class CncMachineButton : MonoBehaviour
{
    public bool IsInteractable { get; set; }
    public event Action PressEvent;
    public void Press()
    {
        if (IsInteractable)
            PressEvent?.Invoke();
    }
}

public class CncMachineButtonManager : MonoBehaviour
{
    [SerializeField] private CncMachineButton button1;
    [SerializeField] private CncMachineButton button2;

    private void OnEnable()
    {
        button1.PressEvent += OnButtonOnePress;
        button2.PressEvent += OnButtonTwoPress;
    }

    private void Start()
    {
        button1.IsInteractable = true;
    }

    private void OnDisable()
    {
        button1.PressEvent -= OnButtonOnePress;
        button2.PressEvent -= OnButtonTwoPress;
    }

    private void OnButtonOnePress()
    {
        button2.IsInteractable = true;
    }

    private void OnButtonTwoPress()
    {
        // Do stuff
    }
}

public class ExampleButtonPress : MonoBehaviour
{
    public void ButtonPresser(Ray ray)
    {
        RaycastHit hit;

        if (Physics.Raycast(ray, out hit, 100))
        {
            var button = hit.transform.GetComponentInChildren<CncMachineButton>();
            
            if (button != null)
            {
                button.Press();
            }
        }
    }
}