[SOLVED] Changing variables in nearest object by script (drag&drop script)

I want to create one script for the object drag and drop function. I made it like this:

public class PlayerPush : MonoBehaviour
{

    private GameObject box;
    private BoxPull boxpull;
    private int enabledbox = 0;
    private float distance;
    private float minimumDistance = 1.2f;
    private bool Epressed = false;

    void Start()
    {
        box = GameObject.FindWithTag("Pushable");
    }
    void Update()
    {
        distance = Vector3.Distance(box.transform.position, transform.position);
        if (distance <= minimumDistance)
        {
            if (Input.GetKeyDown(KeyCode.E))
            {
                if (Epressed == false)
                {
                    Epressed = true;
                    boxpull = box.GetComponent<BoxPull>();
                    boxpull.jointspring = 1;
                }
                else if (Epressed == true)
                {
                    Epressed = false;
                    boxpull = box.GetComponent<BoxPull>();
                    boxpull.jointspring = 0;
                }
            }
        }
    }
}
public class BoxPull : MonoBehaviour
{
    public int jointspring = 0;
    public FixedJoint2D springjoint;

    void Start()
    {
        springjoint = gameObject.GetComponent<FixedJoint2D>();
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        if(jointspring == 1)
        {
            springjoint.enabled = true;
        }
        if(jointspring == 0)
        {
            springjoint.enabled = false;
        }
    }
}

and well… it works fine, but only for one object. If there is two same objects… script works only in one, in the other one doesn’t work at all…

How can I change it to make it work on all objects?

public class PlayerPush : MonoBehaviour
{
    private GameObject[] boxes;
    private BoxPull boxpull;
    private int enabledbox = 0;
    private float distance;
    private float minimumDistance = 1.2f;
    private bool Epressed = false;
    void Start()
    {
        boxes = GameObject.FindGameObjectsWithTag("Pushable");
    }
    void Update()
    {
        foreach(var box in boxes) {
            distance = Vector3.Distance(box.transform.position, transform.position);
            if (distance <= minimumDistance)
            {
                if (Input.GetKeyDown(KeyCode.E))
                {
                    if (Epressed == false)
                    {
                        Epressed = true;
                        boxpull = box.GetComponent<BoxPull>();
                        boxpull.jointspring = 1;
                    }
                    else if (Epressed == true)
                    {
                        Epressed = false;
                        boxpull = box.GetComponent<BoxPull>();
                        boxpull.jointspring = 0;
                    }
                }
            }
        }
    }
}

But this will lead to the fact that you could grab multiple boxes at once if close enough.
You should look for objects near you with some collider or something and take the closest one or something like that.

1 Like

I just got to it myself, but thanks so much anyway ;]