- Home /
Why am I getting a "NullReferenceException" for a Instantiate line?
I've had this weird problem I've been trying to figure out for a few hours, I gave up and was wondering if you guys could help. On this line "`var coconut = Instantiate (coconutprefab, Vector3(transform.localPosition.x, transform.position.y + 2.5, transform.localPositionn.z - 2.0), Quaternion.identity);`" I get a NullReferenceException error. However if i change "`Vector3("stuff")`" to "`Vector3(0,0,0)`" it works. I have made sure all the variables have been connected to objects. This was working fine this morning, I don't know what happened.
function sinstunned() {
stuncooldown = true;
if (coconutcount.coconutsCollected > 0 && spawnerwait == false) {
spawnerwait = true;
coconutcount.coconutsCollected = coconutcount.coconutsCollected - 1;
var coconut = Instantiate (coconutprefab, Vector3(player.transform.localPosition.x, player.transform.position.y + 2.5, player.transform.localPositionn.z - 2.0), Quaternion.Inverse(coconutprefab.transform.rotation));
coconut.transform.rotation = Quaternion.Inverse(player.transform.rotation);
coconut.GetComponent(CollectCoconut).player = cocoCharController;
coconut.animation["ArmatureAction"].speed = -1;
coconut.animation["ArmatureAction"].time = coconut.animation["ArmatureAction"].length;
coconut.animation.Play("ArmatureAction");
}
walkscript.enabled = false;
thirdcharmotor.enabled = false;
player.animation.Play("OrcStun2");
yield WaitForSeconds (2.0);
walkscript.enabled = true;
thirdcharmotor.enabled = true;
OrcStun = false;
yield WaitForSeconds (1.0);
stuncooldown = false;
spawnerwait = false;
}
The most likely issue is that 'coconutprefab' has not be initialized. You don't show the code for initialization, so we cannot check that part of the code. If you are initializing it through drag and drop in the Inspector, and if you verify the value is set in the Inspector, check to make sure you don't have a second copy of the script attached elsewhere.
Yeah I used drag and drop, I made sure it was in there. Here is the full script:
var dead = false;
var player : GameObject;
var shark : GameObject;
var killcam : Camera;
var thirdPCam : Camera;
var FirstPCam : Camera;
var OrcStun = false;
var deadgui : GUIText;
var mousescript : $$anonymous$$ouseLookFpp;
var thirdPCamscript : ThirdPersonCamera;
var charmotor : Character$$anonymous$$otor;
var thirdcharmotor : ThirdPersonController;
var fpsscript : FPSInputController;
var CameraSwap : CameraSwap;
var walkscript : Walk;
var stuncooldown = false;
var coconutcount : TotalCoconuts;
var coconutprefab : GameObject;
var cocoCharController : CharacterController;
var spawnerwait = false;
function Update () {
if (player.transform.position.y <= 15) {
dead = true;
shark.transform.position.x = player.transform.position.x;
shark.transform.position.z = player.transform.position.z;
shark.transform.position.y = 6;
player.active = false;
mousescript.enabled = false;
killcam.enabled = true;
thirdPCam.enabled = false;
FirstPCam.enabled = false;
thirdPCamscript.enabled = false;
charmotor.enabled = false;
thirdcharmotor.enabled = false;
fpsscript.enabled = false;
CameraSwap.enabled = false;
deadgui.enabled = true;
}
if (OrcStun == true) {
sinstunned();
}
}
function sinstunned() {
stuncooldown = true;
if (coconutcount.coconutsCollected > 0 && spawnerwait == false) {
spawnerwait = true;
coconutcount.coconutsCollected = coconutcount.coconutsCollected - 1;
var coconut = Instantiate (coconutprefab, Vector3(player.transform.localPosition.x, player.transform.position.y + 2.5, player.transform.localPositionn.z - 2.0), Quaternion.Inverse(coconutprefab.transform.rotation));
coconut.transform.rotation = Quaternion.Inverse(player.transform.rotation);
coconut.GetComponent(CollectCoconut).player = cocoCharController;
coconut.animation["ArmatureAction"].speed = -1;
coconut.animation["ArmatureAction"].time = coconut.animation["ArmatureAction"].length;
coconut.animation.Play("ArmatureAction");
}
walkscript.enabled = false;
thirdcharmotor.enabled = false;
player.animation.Play("OrcStun2");
yield WaitForSeconds (2.0);
walkscript.enabled = true;
thirdcharmotor.enabled = true;
OrcStun = false;
yield WaitForSeconds (1.0);
stuncooldown = false;
spawnerwait = false;
}
Answer by getyour411 · Feb 10, 2014 at 04:47 AM
Looks like you misspelled position a few times (positionn)
Wow that's embarrassing. Thanks for picking that up.
Your answer
![](https://koobas.hobune.stream/wayback/20220613133045im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Instantiating Particle System and Editing Base Settings 0 Answers
Destroy/Kill AI, Without Errors? 0 Answers
"GameObject.Find();" not working 2 Answers
beginner question- several errors 1 Answer