The prefab you want to instantiate is null.

In my scene, I have a prefab of a particle (also tested it with a plain sphere to no avail) that I am trying to instantiate at the collision of a ray (as a bullet explosion).

I figured I would get this error if the prefab didnt contain any objects but that is not the case. (The prefab icon is blue)

This is just another flaw in my code, I bet, that results from my lack of experience. Thanks in advance for helping!

Ive attached this script to the object that is shooting the ray that results in the collision.

var gun : Transform;
var layerMask = 1 << 8;
var loc : Vector3; 
var bulletspark : Transform;

function Update () {

//position of the gun in the world
var gunPosition : Vector3 = gun.position;
// distance calculation assumes camera has 0, 0, 0 rotation
var distanceFromCamera : float = gunPosition.z - camera.main.transform.position.z;
var screenPos : Vector3 = Input.mousePosition;
screenPos.z = distanceFromCamera;
// calculate where in the game-plane the bullet must go to
var worldPos : Vector3 = Camera.main.ScreenToWorldPoint(screenPos);
// calculate bullet direction...
var dir : Vector3 = (worldPos - gunPosition).normalized;

if (Input.GetButtonDown ("Fire1")) {
var hit: RaycastHit;
    if (Physics.Raycast (gunPosition, dir, hit)) { //cast the ray
    var rot : Quaternion = Quaternion.FromToRotation(Vector3.up, hit.normal); //determine the rotation of the collision
    loc=hit.point; //determined the location of the collision
    loc.z = 0;
    Debug.DrawRay (gunPosition, dir);//debug
    Instantiate(bulletspark, loc, rot);//create sparks

From everything you've described so far, it would appear that the particular instance you're examining is set up entirely correctly.

Which leads me to think, could it be that you've accidentally dropped that script onto some other GameObject in your hierarchy too, without realising? If so, it may be that instance which is throwing the error (as opposed to the one you're inspecting, which seems fine).

Thanks so much for your answers!

My problem is solved.

The problem was actually that I was dragging the Bulletspark prefab into the script and not the instance of the script in the object that fires the raycast.

I predicted that this was a problem related to a misunderstanding in the interface.

Once again thanks for your answers. This community is awesome.

This is checking the obvious, but, you're sure that bulletspark is correctly pointing to the Prefab, in the Editor window? You did a drag/drop, or used the dropdown menu, and it's connected to the public script variable? I've had times when I accidently disconnected a variable without noticing.

Have you changed the type of the variable 'bulletspark' since you made the drag-reference? For example, perhaps it used to be:

var bulletspark : GameObject;

...or something else, when you made the drag-reference, and then maybe you changed the type to:

var bulletspark : Transform;

If so, you have to re-drag the reference even though it appears to still be linked. I can't remember off the top of my head whether this generates the error that you describe, or a different error, but it's one more thing to check.

If you can't remember whether you changed it, just try re-dragging the reference anyway and see if it fixes it. If it does, that was most likely the cause of the problem.