Raycast Collision Detection

I got this code here that is attached to my camera and casts a ray towards my mouse.

var target : Transform;

function Update () {
		var ray : Ray = camera.ScreenPointToRay (Input.mousePosition);
		Debug.DrawRay (ray.origin, ray.direction * 25, Color.yellow);
		
		var hit : RaycastHit;
		
		if(Physics.Raycast(transform.position, transform.forward, hit,25));
		
		  target.position = hit.point;
          target.rotation = Quaternion.LookRotation(hit.normal);
	}

But I am having troubles with the collision. I want it to be that when the ray cast hits the floor or something else to cast down a box in that position. these are 2 code i mixed in from youtube and google. Just need to know, when the ray hits something I want something to happen. Should I use tags or layers for the camera and do I need to do the same for the mouse so the ray wont detect them.

I made another script, thinking this would work, but it’s not. the cube will not go to the ray cast collision point. Might give u guys a better understanding on what I am trying to do.

#pragma strict

var cube : Transform;

private var foundHit : boolean;

function Update() 
{
	//var foundHit : boolean = false;
	var hit : RaycastHit;
	var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
	Debug.DrawRay (ray.origin, ray.direction * 25, Color.yellow);

	if (Physics.Raycast (ray, 100)) 
	{
		foundHit = true;
		cube.position = hit.point;
		print ("Hit something");
		
	}
	
	if(foundHit == true)
	{
	  cube.position = hit.point;
      cube.rotation = Quaternion.LookRotation(hit.normal);
	}
}

Here is another way I am trying, but still cant not get it working.

var cube : Transform;

function Update() 
{
	var hit : RaycastHit;
	var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
	Debug.DrawRay (ray.origin, ray.direction * 25, Color.yellow);

	if (Physics.Raycast (ray, 100)) 
	{
		cube.position = hit.point;
		print ("Hit something");
		
	}
	
}

Your trying to make a target system aren’t you?

yes. its apart of it.

here is my last attempt for tonight.

#pragma strict

var cube : Transform;

function Update() 
{
	var hit : RaycastHit;
	var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
	Debug.DrawRay (ray.origin, ray.direction * 25, Color.yellow);

	if (Physics.Raycast (ray, hit)) 
	{
		cube.transform.position = hit.point;
		print ("Hit something");
		
	}
	
}

well ii tried one more. This script i got from a youtube i found and copied it. it worked greta on his video but i can not get it to work

#pragma strict

var targetPoint:Transform;

function Start () 
{

}

function Update () 
{
  if(targetPoint !=null)
    {
      var cursorRay = Camera.main.ScreenPointToRay(Input.mousePosition);
      var hit : RaycastHit;
      
      if(Physics.Raycast(cursorRay, hit))
        {
          if(targetPoint !=null)
            {
              if(hit.collider.gameObject.tag == "ground")
                {
                  targetPoint.transform.position = hit.point;
                }
            }
        }
    }
}

can someone please help me. im not sure what im doing wrong here.

NVM i got it to work finally. Just forgot to add the ground tag to the floor. the script above works and leaving it for anyone new to scripting or has the same problem i had to use. Gnight. BTW i was not that far off. seems im starting to get it.

Hey Guys,
Raycast hit collisions are giving me problems, on some clicks they get detected but on the other they throw a null pointing error
and clue about that?