Raycast.hit problem

using UnityEngine;
using System.Collections;

public class ConstructionSpacestation : MonoBehaviour
public GameObject Builder; 

// placeholder for what you see on the screen.

private Transform actualBlock;

// layer to be used

LayerMask mask = -1;

private void Start(){

// no block, quit

if(!Builder) return;

// setup our block



private void Update (){

// no block, quit

if(!Builder) return;

// make it dissapear

// If we want it to be there, we need to make sure

// we can hit something with the mouse


// create the ray and hit collector for the Raycast
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit = new RaycastHit();
// debug so that if we dont hit something, we quit

  if (Physics.Raycast(transform.position, transform.forward, 100, mask.value))return;

// if we hit soemthing with the mouse, we make it appear.

    actualBlock.transform.position = hit.point + hit.normal * 0.5f;

// snap the current block to position
    actualBlock.transform.position = new Vector3(


// instantiate the block with a collider.

Instantiate(Builder, actualBlock.position, transform.rotation);


// no getkeydown, just get key, so you can use it in a combo

if(Input.GetKeyDown(KeyCode.Mouse1) && Input.GetKey(KeyCode.LeftAlt)){

if(hit.transform.gameObject != null)

if(hit.transform.gameObject != gameObject)




It keeps telling me there is a NullReferenceException when I changed the

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

and before that I was getting a “Use of unassigned local variable `hit’” when I had “RaycastHit hit;” whats the problem?!

Thanks in advance.

You have the hit variable, but there’s nothing stored in it.

Take a look at this → Unity - Scripting API: Physics.Raycast.
The RaycastHit is a structure to used to get information back from a raycast, but you are not storing any information in it.