Question by
Sloand · Sep 25, 2015 at 02:35 PM ·
2dtouchprefab-instancedestroy-clones
Destroying 2D clones on touch
So I have this game where it continually instantiates objects. When you touch on one of them only that one should get destroyed. I want just one of them to be destroyed. Is this possible? thx.
Comment
Question not clear.
What may touch them ? Finger of the user (on mobile) ? Other object ?
Give details please, and the code you've tried so far
using UnityEngine; using System.Collections;
public class touch : $$anonymous$$onoBehaviour {
void Update()
{
for (int i = 0; i < Input.touchCount; ++i) {
if (Input.GetTouch(i).phase == TouchPhase.Began) {
RaycastHit2D hitInfo = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.GetTouch(i).position), Vector2.zero);
// RaycastHit2D can be either true or null, but has an implicit conversion to bool, so we can use it like this
if(hitInfo)
{
Destroy(gameObject);
// Here you can check hitInfo to see which collider has been hit, and act appropriately.
}
}
}
if (Input.Get$$anonymous$$ouseButtonDown (0)) {
Destroy(gameObject);
}
}
}
Your answer
Follow this Question
Related Questions
Android 2D slingshot with position relative to where the user touched? 0 Answers
Any one help me in this script? 0 Answers
Tetris grid 1 Answer
2D touch - Why is this movement so jittery? 1 Answer
Different colliders in prefabs 0 Answers