So, my problem is I have a collider gameobject (cube) that is a child of the bush I want to destroy. When I run this script (pressing E) it destroys all gameobjects instead of just one. Please help, I am very confused.
using UnityEngine;
using System.Collections;
public class Collect : MonoBehaviour {
public GameObject UIText;
void Start() {
UIText.SetActive (false);
}
void OnTriggerEnter() {
UIText.SetActive (true);
}
void OnTriggerExit() {
UIText.SetActive (false);
}
void Update() {
if (Input.GetKeyDown (KeyCode.E)) {
Ray ray = Camera.main.ScreenPointToRay (new Vector2 (Screen.width / 2, Screen.height / 2));
RaycastHit hitInfo;
if (Physics.Raycast (ray, out hitInfo, 3.5f)) {
if (hitInfo.transform.tag == "Bush") {
UIText.SetActive(false);
Destroy(gameObject);
Destroy(hitInfo.transform.gameObject);
}
}
}
}
}
Thank you very much!