x


Need help with bullet/gun scripting

Hi!! Im new to unity. I have my gun script, spawn point, bullet, and gun. heres my script.

var Bullet: Transform; function Update () { if (Input.GetButtonDown ("Fire1")){

var bullet=Instantiate(Bullet,GameObject.Find("Gun_Spawn").transform.position,Quaternion.identity);
bullet.rigidbody.AddForce(transform.forward *2000);

}

} but every time I hit Fire1 (Left mouse) the bar at the bottom of the screen says, Theres no rigidbody attached to clone, script still searching?? I dont know what to do?? and I also need a script to destroy the bullet. Can any body help?? Thank you for the time.

more ▼

asked Apr 13 '11 at 05:16 PM

Tony 5 gravatar image

Tony 5
17 5 6 9

one question per post but in answer to your destroy question http://lmgtfy.com/?q=unity3d+destroy+gameobject

Apr 13 '11 at 05:21 PM AngryOldMan
(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

You are saying: bullet.rigidbody. But the bullet doesn't have one. Unity3D throws you a NullReferenceException. To solve this: add a new RigidBody component.

If your bullet is a prefab, you can just add it in your editor.

If it is a class that you are creating from code somehow, use a script:

bullet.addComponent(new RigidBody());

will probably work.

more ▼

answered Apr 13 '11 at 05:22 PM

Marnix gravatar image

Marnix
1.3k 21 31 45

(comments are locked)
10|3000 characters needed characters left

well logic would dictate that you should add a rigidbody component to the prefab that your script is trying to instantiate

more ▼

answered Apr 13 '11 at 05:19 PM

AngryOldMan gravatar image

AngryOldMan
2.6k 12 21 47

(comments are locked)
10|3000 characters needed characters left

The problem is that your bullet prefab does not have rigidbody attached.

just to remind you also in unity variables should be started with small letters.

more ▼

answered Apr 13 '11 at 05:23 PM

Kourosh gravatar image

Kourosh
1.5k 47 53 65

why should unity variables be named with small letters? I have loads of codes with variables with a number of case differences in them and it doesnt make a difference to how they perform

Apr 13 '11 at 05:41 PM AngryOldMan

Sorry i meant it's better to start variables with small letter. It's not that it won't work if you use Upper case, it's just a scripting discipline so they are not mistaken with functions which are usually started with Upper case. here is what i found: http://forum.unity3d.com/threads/6903-Is-there-a-recommended-naming-convention

and also wherever i checked in unity's scripting manual you'll see the same rule being applied.

Apr 13 '11 at 06:04 PM Kourosh
(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:

x5087
x3461
x3334
x1175
x446

asked: Apr 13 '11 at 05:16 PM

Seen: 2702 times

Last Updated: Apr 13 '11 at 05:16 PM