How to enable the script after clicking the mouse/touch?^
Hello, how to enable this script after the mouse click /touch ? :3
using UnityEngine; using System.Collections;
public class yuniti : MonoBehaviour {
void OnTriggerEnter2D(Collider2D col)
{
{
if (col.gameObject.tag == "frufii")
{
Destroy(col.gameObject);
}
}
}
}
Have you set the Collider2D in the object you want to destroy to is trigger
I have another script that allows to drag and drop objects by touch and the object on which these scripts are configured . How can I do that this script was included after the click / touch (on and off after the 2nd), which means during the drag and drop? ^ thank you :3
Answer by UnityCoach · Jan 24, 2017 at 09:59 PM
OnMouseDown () will be executed even if the script is disabled, so :
private void OnMouseDown ()
{
enabled = true;
}
Note the object needs a collider, and you have to click on the object. (not sure this is what you want).
Thank you ^how to improve this script?) using UnityEngine; using System.Collections;
public class yuniti : $$anonymous$$onoBehaviour {
private void On$$anonymous$$ouseDown()
{
enabled = true; }
void OnTriggerEnter2D(Collider2D col)
{
{
if (col.gameObject.tag == "frufii")
{
Destroy(col.gameObject);
}
}
}
}
Well, it's fairly simple, what do you want to optimise? Btw, I believe OnTriggerEnter2D () is called even if the script is disabled too. So you want to check if the script is enabled, like :
public class yuniti : $$anonymous$$onoBehaviour
{
private void On$$anonymous$$ouseDown()
{
enabled = true;
}
void OnTriggerEnter2D(Collider2D col)
{
if (enabled && col.gameObject.tag == "frufii")
{
Destroy (col.gameObject);
}
}
}
^ye:3 I have also this script ,that allows to drag and drop objects by touch: using UnityEngine; using System.Collections;
public class Touch : $$anonymous$$onoBehaviour { private float dist; private Vector3 v3Offset; private Plane plane;
void On$$anonymous$$ouseDown()
{
plane.SetNormalAndPosition(Camera.main.transform.forward, transform.position);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float dist;
plane.Raycast(ray, out dist);
v3Offset = transform.position - ray.GetPoint(dist);
}
void On$$anonymous$$ouseDrag()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
float dist;
plane.Raycast(ray, out dist);
Vector3 v3Pos = ray.GetPoint(dist);
transform.position = v3Pos + v3Offset;
}
}
how to unite these two scripts that first turned on while dragging (frufii - is a drag object) on and off? Thank youu: 3
Your answer
Follow this Question
Related Questions
How to detect a touch inside a GUI 0 Answers
How to get touch's positon when I hold the button?? 2 Answers
Is it possible to create a touch input on spawned particles? 0 Answers
Adding points for every gameObject on the scene instead of adding points for the destroyed one 0 Answers
How to detect with colliders while drawing line by line renderer? 1 Answer