x


Pickup Script

i have this script for weapon switching and was thinking couldn't you make it an OnCollisionEnter to change the weapon so here is what i have.

function Awake() 

{ // Select the first weapon SelectWeapon(0); } function Update() { // Did the user press fire? if (Input.GetKeyDown("1")) { SelectWeapon(0); }
else if (Input.GetKeyDown("2")) { SelectWeapon(1); } } function SelectWeapon(index : int) { for (var i=0;i<transform.childCount;i++) { // Activate the selected weapon if (i == index) transform.GetChild(i).gameObject.SetActiveRecursively(true); // Deactivate all other weapons else transform.GetChild(i).gameObject.SetActiveRecursively(false); } }

more ▼

asked Dec 23 '11 at 11:22 PM

conflictbliz gravatar image

conflictbliz
26 14 16 19

You've been a registered user of this site since July and you still haven't caught on to the proper code formatting concept? C'mon.

Dec 23 '11 at 11:37 PM Lo0NuhtiK

i have just not with networking, everytime i try to make a script its either a error saying unknown identifier or somthing silly which i cannot fix.

Dec 24 '11 at 05:22 AM conflictbliz

please format the code that you post in a more readable manner. Select the code and press the 0101 button at the top of the question window to do this.

Jan 14 '12 at 08:17 AM vatsalAtFEI

FTFY:

function Awake() 
{ 
    // Select the first weapon 
    SelectWeapon(0); 
} 

function Update() 
{ 
    // Did the user press fire? 
    if (Input.GetKeyDown("1")) 
    { 
       SelectWeapon(0); 
    }
    else if (Input.GetKeyDown("2")) 
    { 
       SelectWeapon(1); 
    } 
} 

function SelectWeapon(index : int) 
{ 
    for (var i=0; i<transform.childCount; i++) 
    {
       // Activate the selected weapon 
       if (i == index)   transform.GetChild(i).gameObject.SetActiveRecursively(true);           

       // Deactivate all other weapons 
       else transform.GetChild(i).gameObject.SetActiveRecursively(false); 
    } 
}

Now what exactly it is you're asking for?

Jan 14 '12 at 08:39 AM asafsitner
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

okay. To select a weapon using OnCollisionEnter use this in the FPS controller and in the same script that has the function SelectWeapon :

function OnTriggerEnter(other : Collider){

    if(other.gameObject.tag == "aColliderThatChangesToWeaponNumber3")
        SelectWeapon(3);  
    if(other.gameObject.tag == "aColliderThatChangesToWeaponNumber2")
        SelectWeapon(2);
    //and so on

}

You can either have different tags like aColliderThatChangesToWeaponNumber0, aColliderThatChangesToWeaponNumber1, aColliderThatChangesToWeaponNumber2 and so on that individually tell the FPS controler to change the weapon.

Or its better that you create one more code that can be applied to every collider that can change the weapon of the FPS controller and this code stores a number like 0,1,2 and it can be used like this.

This is lets say this is a code called "colliderScript.js" which is attached to all the colliders that can change the weapon of the player. Plus, mark the colliders as Trigger:

var weaponNumber : int;  //this value can be changed in editor for each collider 
                         //if you put is as 1 then it changes FPS weapon to weapon 1
                         //if its 2 then it changes to weapon 2
function Update(){ //this is nothing
}

Now the code that you have above will have this function. Add this function to the same script that has the selectWeapon function and remove the previous OnTriggerEnter function :

function OnTriggerEnter(other : Collider){

        switch (other.gameObject.GetComponent(colliderScript).weaponNumber){
            case 0 :SelectWeapon(0);
                    Debug.Log("weapon 0 selected");
                    break;
            case 1 :SelectWeapon(1);
                    Debug.Log("weapon 2 selected");
                     break;
            // and so on
        } //switch ends
  }  //function ends

I hope this helps! PS : not tried this code in Unity so reply if you get any error(s).

more ▼

answered Jan 14 '12 at 10:01 AM

vatsalAtFEI gravatar image

vatsalAtFEI
734 13 18 25

error Assets/Pickup.js(4,21): BCE0024: The type 'SelectWeapon' does not have a visible constructor that matches the argument list '(int)'.

Jan 14 '12 at 06:19 PM conflictbliz

what is the pickup.js script? Is it the one that has selectWeapon function? Anyways, I made some changes in the code above, go through I just tried the previous code in Unity and found some problem and it worked well when the colliders were triggers.

Jan 14 '12 at 07:18 PM vatsalAtFEI

could you please tell me what script goes on witch because its not that clear, i got the collideScript so when you hit that it should change the weapon. Then there is the selectWeapon script which changes the weapon once collided with the collide script. I just dont know about the function script you told me to add, where does it go, be more specific?

Jan 14 '12 at 10:47 PM conflictbliz

yeah the script with the function SelectWeapon should have the OnTriggerEnter function because the OnTriggerEnter function calls the SelectWeapon and thus they need to be in the same script.

Jan 15 '12 at 06:15 AM vatsalAtFEI

it should work but for SelectWeapon i am getting these errors,

Assets/SelectWeapon.js(4,21): BCE0024: The type 'SelectWeapon' does not have a visible constructor that matches the argument list '(int)'.

Assets/SelectWeapon.js(6,21): BCE0024: The type 'SelectWeapon' does not have a visible constructor that matches the argument list '(int)'.

Assets/SelectWeapon.js(10,17): BCE0005: Unknown identifier: 'other'.

Assets/SelectWeapon.js(11,33): BCE0024: The type 'SelectWeapon' does not have a visible constructor that matches the argument list '(int)'.

Assets/SelectWeapon.js(14,33): BCE0024: The type 'SelectWeapon' does not have a visible constructor that matches the argument list '(int)'.

Jan 15 '12 at 07:18 AM conflictbliz
(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:

x3336
x220
x141

asked: Dec 23 '11 at 11:22 PM

Seen: 1579 times

Last Updated: Jan 15 '12 at 08:40 PM