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?