what I am trying to do is on touch, that specific gameobject will be destroyed. I have created a tag by the name of “city” and placed the respective objects in this tagging system. So here’s my problem, when I run this script and click on an object they are destroyed not by what was touched but in the order they are listed in the scene. What can I do to change this and make it so when clicking on the specific object, it destroys that object. I am sorry if this is any type of repeat question as I have looked all over the web and this form for answers.
public class touch : MonoBehaviour {
public GameObject Tower;
private Ray ray; // cast array
public RaycastHit rayHitInfo = new RaycastHit(); // object that was hit by array
// Update is called once per frame
void Update () {
if (Input.touches.Length <= 0)
{
//if no touch recognised then there will be no interaction
}
else
{
for (int i = 0; i < Input.touchCount; i++)
{
if(Input.GetTouch(i).phase == TouchPhase.Began)
{
ray = Camera.main.ScreenPointToRay (Input.GetTouch(i).position); // creates ray from screen point positoin
if(Physics.Raycast(ray, out rayHitInfo))
{
Tower = GameObject.FindWithTag("city");
Destroy (Tower);
}
}
}
}
}
}