I would like to destroy a gameObject in my scene after so conditions are met, this works but it also destroys the other objects that have the same script even though their conditions have not met yet, hopefully the code explains it a bit:
using UnityEngine;
using System.Collections;
public class selectObject : MonoBehaviour
{
public bool isSelected = false;
public GameObject[] prefab;
public string ballColor = "";
void Update ()
{
if (Input.GetMouseButtonDown (0))
{
RaycastHit hitInfo = new RaycastHit ();
bool hit = Physics.Raycast (Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo);
if (Input.GetMouseButtonDown (0) && hit)
{
Debug.Log("Hit " + hitInfo.transform.gameObject.name);
if (hitInfo.transform.gameObject.tag == "redBall")
{
isSelected = true;
ballColor = "red";
}
else if(hitInfo.transform.gameObject.tag == "blueBall")
{
isSelected = true;
ballColor = "blue";
}
else if(hitInfo.transform.gameObject.tag == "greenBall")
{
isSelected = true;
ballColor = "green";
}
else if(hitInfo.transform.gameObject.tag == "yellowBall")
{
isSelected = true;
ballColor = "yellow";
}
if(isSelected == true && ballColor == "blue")
{
if (hitInfo.transform.gameObject.tag == "bCapsule")
{
//It gets here but i only want the blue ball destroyed, it destroys all balls in the scene right now
Debug.Log ("I'm here");
Destroy (gameObject);
}
}
}
}
}