RaycastHit "variable" in C#

Hi there, I've been trying to use this code wich seems very simple:

Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);

    RaycastHit hit;

    if (Physics.Raycast (ray)) 



And I'm getthing this error message: Use of unassigned local variable `hit'

Could someone please explain what am I doing wrong?

Thanks in advance,

(I'm having so much trouble trying to figure out this documentation in JS to C#, I wish they already had a fully documentated C# reference!)

You need to use one of the versions of Physics.Raycast() that takes a RaycastHit argument. In your case, it would be:

Physics.Raycast(ray, out hit)

This version of the Raycast function will assign a value to hit, so you can (for instance) print hit.point.

Thanks to the help of Daniel Brauer and another friend, the actual code should look like this:

Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);

    RaycastHit hit = new RaycastHit(); //*

    if (Physics.Raycast (ray, out hit)) //**

            print("true" + hit.point);

*- The RaycastHit type object called "hit" should be initialized with "new"

**- THere should be an "out hit" passed as argument for the Physics.Raycast function so that hit would receive a value