Raycast Tap Help

I have this Script:

private var hit : RaycastHit;
private var ray : Ray;//ray we create when we touch the screen

function FixedUpdate () {
    if(iPhoneInput.touchCount == 1) {
        ray = Camera.main.ScreenPointToRay(iPhoneInput.touches[0].position);
        Debug.DrawLine(ray.origin,ray.direction * 10);
        if(Physics.Raycast(ray.origin, ray.direction * 10,hit)){
             if(!gameObject.rigidbody){
             gameObject.AddComponent(Rigidbody);
             }
        }
    }   
}

And I get this error: NullReferenceException UnityEngine.Camera.ScreenPointToRay (Vector3 position) Touchcontrol.FixedUpdate () (at Assets/Standard Assets (Mobile)/Scripts/Touchcontrol.js:8)

This seems to work in unity iphone 1.5 ,but not in unity3.

How can it be fixed?

Or how about another script to tap a collider on iphone)

Thanks!

Well, I believe iPhoneInput was nixed in favor of Input.touches with Unity 3. It seems like you could probably just replace the iPhoneInput.touches[0] with Input.touches.

I'm not near a machine running Unity right now, but I do happen to have a debugging script I wrote a while back when I was learning touch detection.

This may or may not be useful:

var locX : float = 5.5;
var locY : float = .2;
var sizeX : int = 500;
var sizeY : int = 40;

private var beginLocation : Vector2;
private var endLocation : Vector2;

private var isMoving : boolean;
private var isColliding : boolean = false;

// Move cube vars
private var velocity : Vector2;
var smoothTime : float = .15;
private var newCubePos : Vector2;

// Raycast vars
var tagToMove : String = "moveable";
private var hit : RaycastHit;
private var debugRay : Ray;

function Update() 
{   
    for (var touch : Touch in Input.touches) 
    {
        if (touch.phase == TouchPhase.Began) 
        {
            isMoving = true;
            beginLocation = touch.position;

            // Check for gameobject contact
            var boom = Camera.main.ScreenPointToRay (touch.position);
            debugRay = boom;
            if (Physics.Raycast(boom, hit)) 
            {
                if(hit.collider.tag == tagToMove)
                isColliding = true;
            }
            else
            if(hit.collider.tag != tagToMove)
            {
                isColliding = false;
            }
        }   
        if (touch.phase == TouchPhase.Moved && isColliding) 
        {
            newCubePos = Camera.main.ScreenToWorldPoint(touch.position);
            this.transform.Translate(newCubePos.x-this.transform.position.x, newCubePos.y-this.transform.position.y, 0);            
        }
        if (touch.phase == TouchPhase.Ended) 
        {
            endLocation = touch.position;
            isColliding = false;
            isMoving = false;
        }   
    }
}

function OnGUI()
{
    GUI.Label(Rect ( (Screen.width*(locX/10)) - (sizeX / 2), (Screen.height*(locY/10)), sizeX, sizeY), "Touch begin " + beginLocation);
    GUI.Label(Rect ( (Screen.width*(locX/10)) - (sizeX / 2), (Screen.height*(locY/10)+20), sizeX, sizeY), "Touch end " + endLocation);
    GUI.Label(Rect ( (Screen.width*(locX/10)) - (sizeX / 2), (Screen.height*(locY/10)+40), sizeX, sizeY), "Hit Object " + isColliding);
    GUI.Label(Rect ( (Screen.width*(locX/10)) - (sizeX / 2), (Screen.height*(locY/10)+60), sizeX, sizeY), "Touching Screen " + isMoving);
    GUI.Label(Rect ( (Screen.width*(locX/10)) - (sizeX / 2), (Screen.height*(locY/10)+80), sizeX, sizeY), "Obj Pos " + this.transform.position + "  Ray Pos " + debugRay);
}