problem with Physics.Raycast

Hi,

Im using this code on a simple slider as per the help tutorial here - http://www.binpress.com/tutorial/unity3d-touch-input/133

However, there is a problem with the line:

if (Physics.Raycast(screenRay, out hit))

It doesnt seem to be recognising this condition. If I move the print statement above the if statement, it prints just fine, so Im sure its got to do with this line.

Any ideas what might be wrong?

        if(nbTouches > 0)
        {
            for (int i = 0; i < nbTouches; i++)
            {
                Touch touch = Input.GetTouch(i);
             
                if(touch.phase == TouchPhase.Began)
                {
                    Ray screenRay = Camera.main.ScreenPointToRay(touch.position);

                    RaycastHit hit;
                    if (Physics.Raycast(screenRay, out hit))
                    {
                        print("User tapped on game object " + hit.collider.gameObject.name);
                        //handleTap(hit.collider.gameObject);
                    }
                }
             
            }
        }

The object you click…it has a collider?

Hi SubZeroGaming,

Im wondering how I can add a collider? Should I add a box collider exactly the same size as the slider?

    void OnGUI () {

        hSlideValue= GUI.HorizontalSlider(new Rect(25,25,100,30),hSlideValue,0.0f,1.0f);   
 
    }

ooh, are you trying to touch a UI element??

For that, you need to use the Event system and check the current Event e if you’re using the OnGUI system…

which brings up the question, why are you using the OnGUI system? UGUI is 100% better and more efficient.

Im totally confused regarding UGUI.

Also, Ive made another script where I create lots of buttons and resize them at runtime (based on the canvas size). However, I cannot seem to run this with UGUI. Are there any good tuts on dynamically scripting UGUI and also creating prefabs and referencing them in code?

Eh, I’m sure there are.

In the mean time, I believe what you can use is void OnMouseDown(). It’s a unity function that checks if you clicked over a collider or I believe, a UI element. Give that a try.