How to make the same script on multiple objects work just for the object where it is called?

My problem is really simple but I do not know how to search for this and actually find a good topic, so I am sorry if this is a double post.

I have a scene with multiple identical levers which all use one script:

using UnityEngine;

public class LeverScript : MonoBehaviour {

    public GameObject objectToUse;
	
    public void UseObject()
    {
        Debug.Log("Using lever!");        
        Destroy(objectToUse);
    }
}

I also have an USE button on my canvas which calls my UseObject() from the lever. This is supposed to deactivate a forcefield.

using UnityEngine;
using UnityEngine.EventSystems;

public class JoyButtonUse : MonoBehaviour, IPointerUpHandler, IPointerDownHandler
{     
    public void OnPointerDown(PointerEventData eventData)
    {
        Debug.Log("Joybutton Used");
        FindObjectOfType<LeverScript>().UseObject();  
    }

    public void OnPointerUp(PointerEventData eventData)
    {
        throw new System.NotImplementedException();
    }
}

The problem is that even if in interface there are different forcefields attached to each lever as objectToUse, they all call just one and it is really impossible for me to understand why.

Update:
I tried to change my code as follows:
The lever script:

using UnityEngine;

public class LeverScript : MonoBehaviour {

    public GameObject objectToUse;

    private void Update()
    {
        if (JoyButtonUse.usePressed)
        {
            UseObject();
        }
    }

    public void UseObject()
    {
        Debug.Log("Using lever!");        
        Destroy(objectToUse);
    }
}

and the use button script to this:

using UnityEngine;
using UnityEngine.EventSystems;

public class JoyButtonUse : MonoBehaviour, IPointerUpHandler, IPointerDownHandler
{
    public static bool usePressed = false;

    public void OnPointerDown(PointerEventData eventData)
    {
        usePressed = true;
        Debug.Log("Joybutton Used: " + usePressed);       
        
    }

    public void OnPointerUp(PointerEventData eventData)
    {
        usePressed = false;
        Debug.Log("Joybutton released: Pressed is: " + usePressed);
    }
}

public void UseObject()
{
    Debug.Log("Using lever!");        
    Destroy(objectToUse);
}}

so that my button does not know about the in game objects.

But now, all objects are destroyed when using any lever.

How can I make those scripts work independently while being basically the same?
Or is any better optimal solution for my needs?

You second code is fashioned to destroy all objects, so it may be a small comfort to know the computer is doing exactly what you’ve told it to do (that’s a bad attempt at humor). The trick, now, is to figure out how to reference only one of the instances of LeverScript.


The opposite is true of the first code posted, which uses FindObjectOfType, which, as the name implies, finds one object and only calls UseObject on that one that’s found, which will be the first one this ‘find’ call locates, and usually one you might not intend.


So which one? In the second version, Update in LeverScript is being processed at every Update cycle for all scripts that have an Update method. As a result, they are all ‘seeing’ the same static JoyButton.usePressed, and when true, they are all calling UseObject. How would you identify only one of the several LeverScript based objects to be called? This is key. In what way can you determine the one lever you intend for the call to UseObject, and not any other LeverScript object?


I see nothing in your post that helps identify which one. I have to guess to proceed.


Let’s say something happened in the game. Maybe an object contacted the lever to be moved, and so there might be some collision detected. THAT would identify one, and only one of the several levers to be ‘used’.


Maybe there’s something that moves focus from one lever to another. How would you select a particular lever? Maybe instead of clicking a button, you need to click the lever itself?


Your first approach is actually closer, but…maybe you need to use FindObjectsOfTypeAll, which gives you a list of all the lever scripts, and then search through that list of the one and only one you intend to call UseObject.