Before updating to Windows 10 you may need to return your Pro license. More information here

Player Weapon Scripting

I want to have the player start off with the standard 2 weapon deal. Well thats done. But also find other weapons onn the way. How would i go about this when I go to edit the PlayerWeapons script from the tutorial. this is what I got so far it doesnt work though. If thier is a better script please show me what it is, ive been botherd for 5 days now about it.

function Start () {

 // Select the first weapon


function Update () {

 // Did the user press fire?
 if (Input.GetButton ("Fire1"))

 if (Input.GetKeyDown("1")) {
 else if (Input.GetKeyDown("2")) {
 else if (Input.GetKeyDown("3")) {
     if (gameObject("AssultRifle").SetActiveRecursively(true));{
     else if (gameObject("AssultRifle").SetActiveRecursively(false));{


more ▼

asked Apr 01, 2010 at 11:53 PM

avatar image

Trevor 1
81 22 22 30

D: Could you please make the code a little more readable?

Apr 02, 2010 at 12:00 AM The BOOM

I dont know what you want me to do with it thats how most of it was made. selecting a weapon is just plane and simple to begin with.

Apr 02, 2010 at 12:35 AM Trevor 1

He was talking about the code formatting on the site. You need to highlight the code and click the "Code Format" button if you're going to post code here. I fixed this post for you, but in the future don't forget this.

Jun 08, 2010 at 09:53 PM qJake
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Well i do not know if this is the 'best' way but you can do it like i did it.

var weapon: int = 0;

function Update () { if (Input.GetKeyDown("1") { weapon = 0; } if (Input.GetKeyDown("2") { weapon = 1; }

 if (Input.GetButton ("Fire1")){
     if(weapon == 0) {
     //pistol stuff
     if (weapon ==  1) {
     //assault rifle stuff


You can also have 'weapon' be static so when you pick up a new gun it changes it to 3 so you can do:

    if (weapon ==  3) {
    //rocket launcher stuff

If you want to do a Metal Gear Solid thing where once you get a weapon you unlock it forever (i guess its similar in other games its just i like MGS haha) you can do:

var rocketlauncher : boolean = false;

if(rocketlauncher) { if(Input.GetKeyDown("3") { weapon = 3 } }

You can then also have rocketlauncher be static so when you get a rocketlauncher it changes it to true.

Well good luck!

more ▼

answered Jun 08, 2010 at 10:09 PM

avatar image

560 59 61 69

(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



Answers and Comments



asked: Apr 01, 2010 at 11:53 PM

Seen: 6573 times

Last Updated: Jun 08, 2010 at 09:53 PM