- Home /
Getting UnassignedReferenceException even though reference assigned
Followed a VTC tutorial and made a simple script to throw objects. Worked fine till I messed with it. Now I keep getting UnassignedReferenceException - The variable grenadePrefab of 'sc_throw' (my script) has not been assigned. You probably need to assign the grenadePrefab variable of the sc_throw script in the inspector'
I've gone to the Inspector and dragged my grenadePrefab into the default reference area and it shows up there but I still keep getting the same error. (and I'm doing this while in scene view with game stopped, not while playing). I've also done this with and without a Rigidbody component attached to my grenadePrefab but that doesn't fix the issue.
Script is pasted below. Any suggestions as to where I messed up or how to fix?
var speedThrow = 3.0; var grenadePrefab:Transform;
function Update () { // find out if a fire button is pressed if(Input.GetButtonDown("Fire1")) {
if(CollisionsAndRaycast.GRENADE_AMMO >0) { // create the prefab via script //Instantiate prebuilt function takes 3 parameters - the prefab object; the position, and rotation of current camera/associated object var grenade = Instantiate(grenadePrefab, transform.position, Quaternion.identity);
//add force to the prefab
grenade.rigidbody.AddForce(transform.forward * 2000);
CollisionsAndRaycast.GRENADE_AMMO --;
print("You now have " + CollisionsAndRaycast.GRENADE_AMMO + " grenades.");
}
else
{
print("You have no grenades");
}
}
}
Answer by Rob 4 · Dec 23, 2010 at 05:06 PM
Ok - figured it out.
I'd added the grenadePrefab to the script in the Inspector BUT I'd attached the script to the Main Camera of my First Person Controller, and I hadn't ever clicked on the main camera and added the grenadePrefab to the script that shows up listed on the Main Camera.
Error gone now.
Your answer
![](https://koobas.hobune.stream/wayback/20220613092309im_/https://answers.unity.com/themes/thub/images/avi.jpg)