- Home /
Question by
thanhtu150 · Nov 12, 2014 at 03:28 PM ·
objecttouchdestroy
Touch to destroy specific object
Hi, im a new game developer, and i have a problem, i spawn 5 prefabs, but when i touch 1 object to destroy, 4 object are be destroyed ! Now i want to touch 2 object, this 2 object will be destroy, not 5, that all i need. Tks for read, here my code:
void Update ()
{
if (Input.touchCount > 0)
{
RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint((Input.GetTouch(0).position)), Vector2.zero);
if(hit.collider != null)
{
Destroy(hit.transform.gameObject,0.2f);
}
}
}
Comment
Answer by 767_2 · Nov 12, 2014 at 04:04 PM
try this instead
void Update () {
if (Input.GetMouseButtonDown (0)) {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit)) {
Destroy(hit.collider.gameObject);
}
}
}
Answer by HanSoloYolo · Feb 05, 2018 at 05:49 PM
using UnityEngine;
public class OpenBrowserHarmony : MonoBehaviour {
private GameObject harmonyVideo;
// Use this for initialization
void Start () {
harmonyVideo = GameObject.Find("HarmonyVideo");
}
// Update is called once per frame
void Update()
{
foreach (Touch touch in Input.touches)
{
if (touch.phase == TouchPhase.Began)
{
Ray ray;
RaycastHit hit;
ray = Camera.main.ScreenPointToRay(touch.position);
if (Physics.Raycast(ray, out hit, Mathf.Infinity))
{
if (hit.transform.gameObject.name == ("HarmonyVideo"))
{
Destory(gameObject, 0.2f);
}
else
{
Debug.Log("This object is not the Harmony Video");
}
}
}
}
}
}
Your answer
Follow this Question
Related Questions
Android touch 3d Object event 1 Answer
Destroy on touch for iphone 1 Answer
Destroy object if get touched 1 Answer
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
Getting all Object to go next scene. 1 Answer