Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

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

Discussion in 'Scripting' started by ProExCurator, May 4, 2020.

  1. ProExCurator

    ProExCurator

    Joined:
    Jan 2, 2020
    Posts:
    87
    I want to create one script for the object drag and drop function. I made it like this:
    Code (CSharp):
    1. public class PlayerPush : MonoBehaviour
    2. {
    3.  
    4.     private GameObject box;
    5.     private BoxPull boxpull;
    6.     private int enabledbox = 0;
    7.     private float distance;
    8.     private float minimumDistance = 1.2f;
    9.     private bool Epressed = false;
    10.  
    11.     void Start()
    12.     {
    13.         box = GameObject.FindWithTag("Pushable");
    14.     }
    15.     void Update()
    16.     {
    17.         distance = Vector3.Distance(box.transform.position, transform.position);
    18.         if (distance <= minimumDistance)
    19.         {
    20.             if (Input.GetKeyDown(KeyCode.E))
    21.             {
    22.                 if (Epressed == false)
    23.                 {
    24.                     Epressed = true;
    25.                     boxpull = box.GetComponent<BoxPull>();
    26.                     boxpull.jointspring = 1;
    27.                 }
    28.                 else if (Epressed == true)
    29.                 {
    30.                     Epressed = false;
    31.                     boxpull = box.GetComponent<BoxPull>();
    32.                     boxpull.jointspring = 0;
    33.                 }
    34.             }
    35.         }
    36.     }
    37. }
    Code (CSharp):
    1. public class BoxPull : MonoBehaviour
    2. {
    3.     public int jointspring = 0;
    4.     public FixedJoint2D springjoint;
    5.  
    6.     void Start()
    7.     {
    8.         springjoint = gameObject.GetComponent<FixedJoint2D>();
    9.     }
    10.  
    11.     // Update is called once per frame
    12.     void FixedUpdate()
    13.     {
    14.         if(jointspring == 1)
    15.         {
    16.             springjoint.enabled = true;
    17.         }
    18.         if(jointspring == 0)
    19.         {
    20.             springjoint.enabled = false;
    21.         }
    22.     }
    23. }
    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?
     
  2. Noblauch

    Noblauch

    Joined:
    May 23, 2017
    Posts:
    256
    Code (CSharp):
    1. public class PlayerPush : MonoBehaviour
    2. {
    3.     private GameObject[] boxes;
    4.     private BoxPull boxpull;
    5.     private int enabledbox = 0;
    6.     private float distance;
    7.     private float minimumDistance = 1.2f;
    8.     private bool Epressed = false;
    9.     void Start()
    10.     {
    11.         boxes = GameObject.FindGameObjectsWithTag("Pushable");
    12.     }
    13.     void Update()
    14.     {
    15.         foreach(var box in boxes) {
    16.             distance = Vector3.Distance(box.transform.position, transform.position);
    17.             if (distance <= minimumDistance)
    18.             {
    19.                 if (Input.GetKeyDown(KeyCode.E))
    20.                 {
    21.                     if (Epressed == false)
    22.                     {
    23.                         Epressed = true;
    24.                         boxpull = box.GetComponent<BoxPull>();
    25.                         boxpull.jointspring = 1;
    26.                     }
    27.                     else if (Epressed == true)
    28.                     {
    29.                         Epressed = false;
    30.                         boxpull = box.GetComponent<BoxPull>();
    31.                         boxpull.jointspring = 0;
    32.                     }
    33.                 }
    34.             }
    35.         }
    36.     }
    37. }
    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.
     
    ProExCurator likes this.
  3. ProExCurator

    ProExCurator

    Joined:
    Jan 2, 2020
    Posts:
    87
    I just got to it myself, but thanks so much anyway ;]