I am new to Unity. I am trying create a game where an object drops down randomly, when its touch it pops.
I got it to drop randomly but when touch it can touch on the screen anywhere. What I need to do is detect the gameObject that is touched. The following is my script.
function Update () {
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Input.touchCount > 0 Input.GetTouch(0).phase == TouchPhase.Began)
{
if (Physics.Raycast (ray, hit, 100))
{
if(hit.collider.gameObject.tag == "greenCap")
{
Debug.Log("green");
animationTarget.Play("pop");
}
}
}
}
What I need to do to detect my gameObject.
Thank you.
you already detect it: hit.collider.gameObject 
Thanks for your replay dremora 
but since I have the same object been cloned randomly, how do I detect that the object that I touched is the object that needs to pop.
cause now when i touch all the object pops… please guide.
if all pop then you did something wrong on the code handling the pop, like having no pop on each target object on its own (above code uses some general animationTarget object, nothing thats in relation to the hit object at all for example)
there is no separate code to handle the pop… it is just animation of the object… i have done a bit of changes on the code. but now when i touch the screen a few of the object is popped.
i am not so good with the code, what i am asking is how to know that i am touching this object out of 10 same object in the gameplay.
or when i touch is on a particular object.
function Update () {
var hit: RaycastHit;
if (Input.touchCount > 0 Input.GetTouch(0).phase == TouchPhase.Began)
{
//check if colliding
if(Physics.Raycast(transform.position, transform.forward, hit,1))
{
//collide with object
if(hit.collider.gameObject.tag == "greenCap")
{
hit.collider.gameObject.animation.Play("pop");
Destroy(this.gameObject);
}
}
}
}
hit.collider is the object you touch (or better which the raycast detects as touched)
If you ensured that each object has an own collider, this on its own will take care of it.
Problems you have there though are:
- you play the animation and try to destroy right after. will not work, you will need to use a coroutine and wait for the animation to end before you destroy it
- you destroy the wrong game object, the one that checks the touches (your input handler likely) instead of hit.collider.gameObject

thanks dreamora…
but i dont understand how to use a coroutine.
wait for the animation to finish and then destroy it i did the following code
function dead()
{
yield WaitForSeconds(3);
Destroy(this.gameObject);
}
you say i destroy the wrong game object, yes that is true, but i do i script to destroy the correct object?
sorry for asking a lot of question, i am still learning this wonderful unity to create a simple game.
thank you again 
To destroy the correct object, use hit.collider.gameObject (or if you use a coroutine, send the game object through as well), if you use this.gameObject then the object calling
just cause its simpler to explain with code
function Update () {
var hit: RaycastHit;
if (Input.touchCount > 0 Input.GetTouch(0).phase == TouchPhase.Began)
{
//check if colliding
if(Physics.Raycast(transform.position, transform.forward, hit,1))
{
//collide with object
if(hit.collider.gameObject.tag == "greenCap")
{
hit.collider.gameObject.animation.Play("pop");
StartCoroutine(DestroyObject(hit.collider.gameObject));
}
}
}
}
function DestroyObject( target : GameObject )
{
while( target.animation.isPlaying )
yield;
Destroy( target );
}
wow that made a difference…
i have another question, this code
Physics.Raycast(transform.position, transform.forward, hit,1)
the number ‘1’, it represents the distance of the input to the object right?
but when i touch the screen on the left the object on the right pops.
and if i try touching the objects that are bouncing in the air there is no effect. what am i missing here?
1 represents the distance you want to send the ray along to detect a touch at maximum, so kind of yes 
as for not getting the right object: potentially you raycast from the wrong point or into the wrong direction.
use Debug.DrawLine from transform position to transform position + transform.forward and then touch and while doing so pause the playback and look into the editor view (or have editor and gameview available and check it that way)
question is anyway why you have transform forward there etc and don’t use the function to generate a ray from the camera for example
i tried using this
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
instead of the transform.position, but it did not work.
I use the Debug.DrawLine but I don’t see anything on the editor. 
I am having an issue here now, when i destroy the object, which i refer to my drop down randomly script. once the main object is destroy i get error missing reference.I know this because i am referring to that object to populate the others. How do i avoid the main object not to be destroyed.
EDIT:
I got the DrawLine, i can see the lines on the editor now. It still pop a few capsule in one touch.