Hello, so I needed a script for destroying an object when it is touched on. This game is 2D and for the android. The last script I used was this -
using UnityEngine;
using System.Collections;
public class Break : MonoBehaviour {
public GameObject cubes;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
if (Input.touchCount >0)
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
if (Physics.Raycast(ray, out hit))
if (hit.collider.gameObject.tag =="new")
{
Destroy(cubes);
//cubes.rigidbody.AddForce(Vector3.forward * Time.deltaTime *500);
}
}
}
}
I don’t understand why it isn’t working. I’ve changed the tag of the object to new and it still won’t work. Any help please?
private var mouseEntered : boolean;
function Start () {
// ur code here
}
function Update (){
// ur code here
if (mouseEntered && Input.GetMouseButtonDown(0)){
Destroy(gameObject);
}
}
function OnMouseEnter(){
mouseEntered = true;
}
function OnMouseExit(){
mouseEntered = false;
}
This code works fine also for mobile platforms (thx for default converting mouse handler functions by Unity), but only for one-touch apps.
Hi guys, I´m new in this… and I used kind the same code… but I want to measure the time of every touch of an object… it works fine with one… but it doesn´t measure the second touch so it can be destroy in certain time. here is the code:
void Update ()
{
int nbTouches = Input.touchCount;
if (nbTouches > 0)
{
print(nbTouches + " touch(es) detected");
for (int i = 0; i < nbTouches; i++)
{
Touch touch = Input.GetTouch(i);
ray = Camera.main.ScreenPointToRay(touch.position);
print("Touch index " + touch.fingerId);
if (Physics.Raycast(ray, out hit, Mathf.Infinity))
{
if (hit.collider.tag == "Circle")
{
touchTime = Time.fixedTime;
Debug.Log("TIME " + touchTime);
if (touchTime > holdTime)
{
touchTime = 0;
Debug.Log("Destroy Object in " + touchTime);
Destroy(hit.collider.gameObject, lifeTime);
}
}
}
}
}
}