How to tell colliders apart? (iPhone)

Alright. Posted this question a while ago: http://answers.unity3d.com/questions/239078/ios-touch-triggers-the-wrong-object.html

My problem now is similar, yet different.

In my scene is a teapot which spins when tapped and a rocket which launches when tapped.

My problem is that which ever object I tap, the teapot spins AND the rocket launches.

Iv’e tried some stuff to tell the colliders apart, but can’t get it to work. I know someone awesome in here will help me out :slight_smile:

Here’s the script for the teapot:

using UnityEngine;
using System.Collections;

public class TouchBlueTeapot : MonoBehaviour {
	
	public Animation m_animation;
	
	// Use this for initialization
	void Start () {
		Debug.Log("started");
	}
	
	// Update is called once per frame
	void Update () {
	foreach (Touch touch in Input.touches)
			
{
    Ray ray = Camera.main.ScreenPointToRay(touch.position);
			Debug.Log("RAY:" + ray.direction);
    
    if (touch.phase == TouchPhase.Began)
    {
		Debug.Log("touchphase position:" + touch.position);
        RaycastHit hit = new RaycastHit();
		
				
        if (Physics.Raycast(ray, out hit, 1000))
        {

            
            Debug.Log("touched " + hit.transform.name);
	
			
					gameObject.animation.Play();
					
        }
    }
}
	
	
	}
}

And here’s the code for the rocket:

using UnityEngine;
using System.Collections;

public class Rocket : MonoBehaviour {

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {


foreach (Touch touch in Input.touches)

{
    Ray ray = Camera.main.ScreenPointToRay(touch.position);

    
    if (touch.phase == TouchPhase.Began)
    {

        RaycastHit hit = new RaycastHit();

        if (Physics.Raycast(ray, out hit, 1000))
        {

          
			rigidbody.AddForce(0, 1200, 0);

        }
    }
}
}

}

In the teapot code you are already printing the name of the transform that you hit. You could compare that to the teapot’s name, and only play the animation if they match… But the simpler answer is to compare the transform that was hit to the teapot’s transform directly.

if (hit.transform = this.transform)
{
}

Similarly, the rocket isn’t checking the transform that was hit to see WHO was hit.

(There is something inefficient in your approach: both the rocket and the teapot are firing a ray to see if they were tapped. No big deal for a couple objects, but if you had a lot of objects it would start to add up. I would instead pick one object to be your touch manager. In it’s update it would fire a single ray, get back the list of object(s) that were touched, and send each one a message to tell it to do something.)