x


Whats wrong with my Aim Down Sights script???

Hi!!! Just got my script off unifycommunity, the wiki unity website, i had to do a few changes, and after i did them a get 1000's of errors saying:

NullReferenceException: Object reference not set to an instance of an object?? oh and heres my script!

var gun : Transform; var nextPos = 0.0; var nextField = 40.0; var nextPos2 = -0.2; var dampVelocity = 0.4; var dampVelocity2 = 0.4; var dampVelocity3 = 0.4;

function Update () { var newPos = Mathf.SmoothDamp(gun.transform.localPosition.x, nextPos, dampVelocity, .3); var newField = Mathf.SmoothDamp(Camera.main.fieldOfView, nextField, dampVelocity2, .3); var newPos2 = Mathf.SmoothDamp(gun.transform.localPosition.y, nextPos2, dampVelocity3, .3);

gun.transform.localPosition.x = newPos; gun.transform.localPosition.y = newPos2; Camera.main.fieldOfView = newField;

if (Input.GetButton("Fire2")) { //adjust viewpoint and gun position nextField = 40.0; nextPos = 0.0; nextPos2 = -0.2;

   //slow down turning and movement speed
   GetComponent("MouseLook").sensitivityX = 2;
   camera.main.GetComponent("MouseLook").sensitivityX = 2;
   camera.main.GetComponent("MouseLook").sensitivityY = 2;

} else { //adjust viewpoint and gun position nextField = 60.0; nextPos = 0.5; nextPos2 = -0.4;

   //speed up turning and movement speed
   GetComponent("MouseLook").sensitivityX = 6;
   camera.main.GetComponent("MouseLook").sensitivityX = 6;
   camera.main.GetComponent("MouseLook").sensitivityY = 6;

} }

Thanks for your time!

more ▼

asked Apr 20 '11 at 07:05 PM

Tony 5 gravatar image

Tony 5
17 5 6 9

It's really hard to read through scripts when they are not formatted. Please format your script by highlighting it and pressing the "011001" icon.

Apr 20 '11 at 07:21 PM Kourosh

Have you assigned your gun model to the gun variable in the inspector yet?

Apr 20 '11 at 07:23 PM Xedfire 1
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

You also need to have the mouselook script on the same object I believe.

more ▼

answered Apr 20 '11 at 07:41 PM

burgunfaust gravatar image

burgunfaust
260 5 7 17

From the script wiki:

Usage

Place your gun model as a child of the Main Camera in the default FPSWalker. It's position should be x(0.5), y(-0.4), and whatever z position looks best.

You may want to adjust the scale of your model to whatever looks best in this position as well.

Set up an input button named "Sights". You can map this to whatever key or mouse button you want. Mine is set to left shift.

Attach the following script to the First Person Controller.

Drag your gun model (child of the main camera) into the slot for "Gun" on the script.

Apr 20 '11 at 07:49 PM burgunfaust
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3340
x1176
x446
x3

asked: Apr 20 '11 at 07:05 PM

Seen: 1282 times

Last Updated: Apr 20 '11 at 07:05 PM