weapon pickup and equiping it to FPS player

I know this has been asked but I can't find an answer that suits what I'm trying to do.

I have a setup where my FPS player starts off in a house with no weapon selected.

He finds on on the wall and picks it up. I'm using this as my pickup for the weapon:

var gotShotgun : boolean = false;

var gunPickupSound : AudioClip;

function Start (){
    //playerWeapons is the players selectable weapons
    playerWeapons = this.GetComponent("PlayerWeapons");

function OnTriggerStay (){

    if (Input.GetButtonDown("Pickup")){
        gotShotgun = true;

    //Play Gun Pickup Sound
    if (gunPickupSound)
    AudioSource.PlayClipAtPoint(gunPickupSound, transform.position);

    print("You Picked Up The Double Barel Shotgun");

The gun dissapears from the wall and it prints out that you have "You Picked Up The Double Barel Shotgun"

So far so good.

What I can't figure out is how to get it to activate and load up the shotgun from my PlayerWeapons script

var weapons : GameObject[];
var selectedWeapon : int;

function Awake () {
    // Select the first weapon

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

if (Input.GetKeyDown("1") && weapons.length >= 1) {
            selectedWeapon = 0;
    } else if (Input.GetKeyDown("2") && weapons.length >= 2) {
            selectedWeapon = 1;
    } else if (Input.GetKeyDown("3") && weapons.length >= 3) {
            selectedWeapon = 2;
    } else if (Input.GetKeyDown("4") && weapons.length >= 4) {
            selectedWeapon = 3;
    } else if (Input.GetKeyDown("5") && weapons.length >= 5) {
            selectedWeapon = 4;


function SelectWeapon (index : int) {
    for (var i=0;i<transform.childCount;i++)    {
        // Activate the selected weapon
        if (i == index)
        // Deactivate all other weapons

3 months of trying to learn this code stuff and a lot of it still makes my brain hurt lol. Anyone know how I can make this work?

more ▼

asked May 12, 2011 at 08:39 PM

Michael 12 gravatar image

Michael 12
132 270 189 198

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

2 answers: sort oldest

First of all. This code:

            selectedWeapon = 0;

Is a good candidate for a function:

function selectWeaponByIndex(var i:int):void
    if(!weapons[i].gameObject.GetComponent("weaponLockout").isLocked) {
            selectedWeapon = i;

So when you want weapon "1" you can just call selectWeaponByIndex(1).

Now.. when you pick-up the shotgun you should broadcast a message that you did. Have your PlayerWeapons script respond to this by selecting the shotgun.

more ▼

answered May 12, 2011 at 08:47 PM

flaviusxvii gravatar image

4.9k 47 37 67

Thanks flaviusxvii, not sure I really get what you are doing here, can you perhaps explain in more detail for those of us code challenged ;)

May 12, 2011 at 09:04 PM Michael 12

I don't know what you're asking me to clarify? Functions? Broadcasting? You already have examples of both in your code. Use those as templates.

May 12, 2011 at 09:14 PM flaviusxvii

OK well, like I say, this code stuff a lot I don't get so when I changed my code to what you have here I get an error message that I don't know what to do about:

Assets/WeaponScripts/PlayerWeapons.js(15,10): BCE0044: expecting (, found 'selectWeaponByIndex'.

May 12, 2011 at 09:20 PM Michael 12

http://pastebin.com/idbGRjGK <-- Like this for the function change..

May 12, 2011 at 09:42 PM flaviusxvii

Ok I changed my script to that but it does not equip my gun to my player, so what am I missing? It took me a while just to see what was different about my script and the one you have.

May 12, 2011 at 10:40 PM Michael 12
(comments are locked)
10|3000 characters needed characters left

Try this.

var shotGun = Transform;
private var shotGun : machineGun;

function OnTriggerEnter ()
    Destroy (gameObject);
    if ( machineGun )
        machineGun.enabled = true;

All you have to do now is to make the mesh activate. More detail needed, let me know.

more ▼

answered May 17, 2011 at 03:56 AM

Kashaunzilla gravatar image

29 51 40 48

(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: May 12, 2011 at 08:39 PM

Seen: 3290 times

Last Updated: Jul 18, 2011 at 10:42 PM