Search Unity

Weapon PickUp

Discussion in 'Scripting' started by The3DKnight, Feb 25, 2013.

  1. The3DKnight

    The3DKnight

    Joined:
    Feb 8, 2011
    Posts:
    240
    Hi. I'm having trouble with weapon pick up .I made so far when i collide with weapon collider it shows GUI , and it gets WeaponIndex from that picked up weapon , and adds it to this script.But it won't work i don't know why.
    1) It won't detect collision
    2) How to add weapon number from weapon that i picked up to my pick up script.

    Script is :
    PickUP
    Code (csharp):
    1.  
    2. var ShowGUI : boolean = false;
    3. var PickUpE : Texture2D;
    4. var script :  WeaponIndex;
    5. var Index : int = 0;
    6. var curWeapon : int = 0;
    7.  
    8. //******1)*******
    9. //It won't detect collision between Character and weapon
    10.  
    11. function OnCollisionEnter (collide : Collider) {
    12.     if(collide.gameObject.tag == "Weapons")
    13.     {    
    14.                 //*****2)******
    15.                 //How to add WpIndex number to Index number that i have in this script
    16.  
    17.         script.GetComponent("WeaponIndex").WpIndex = Index;
    18.         ShowGUI = true;
    19.  
    20.     }
    21. }
    22.  
    23. function OnCollisionExit(isExitCollide : Collision)
    24. {
    25.     ShowGUI = false;
    26. }
    27.  
    28. function OnGUI()
    29. {
    30.     if(ShowGUI)
    31.     {
    32.         GUI.DrawTexture(Rect(85,30,100,20), PickUpE);
    33.     }
    34. }
    I used simple for Weapon Index.

    WeaponIndex
    Code (csharp):
    1.  
    2. //Weapon Number
    3. var WpIndex : int = 0;
    4.  
    Anybody knows how to solve this.
    Thanx for your time and help.

    -3DK
     
  2. EvansGreen

    EvansGreen

    Joined:
    Nov 9, 2012
    Posts:
    129
    Hey! Code doesn't make much sense to me.
    1.What object is it attached to?
    2.Why don't you make any reference to the actual WeaponIndex?

    when you say
    Code (csharp):
    1.  
    2. var script :  WeaponIndex;
    3.  
    and then
    Code (csharp):
    1.  
    2. script.GetComponent("WeaponIndex").WpIndex = Index;
    3.  
    aren't you getting the same component you're refering to?

    If I were you, I'd reference the var script to the weaponindex script of the player, and then change it's value. Bear in mind it's a reference value, so it's not copied but pointed to when you do this:
    [CODE
    script = GetComponent("WeaponIndex");
    script.WpIndex = Index;
    [/CODE]

    This would be using your actual code. I think there are less convoluted ways of doing this :).

    Happy coding!
     
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    from the help page for OnCollisionEnter
    You don't mention either the weapon or the char having a rigidbody...
     
  4. The3DKnight

    The3DKnight

    Joined:
    Feb 8, 2011
    Posts:
    240
    Hi, thanks for reply.
    I attached(WeaponPickUp Script) to my Player (Capsule) which has Character Controller . And the weapon that i want to collide with has rigidbody (Use Gravity checked , Is Kinematic unchecked).
    I'm sorry if i didn't make it more clearly
    You see i wanted when my character hits Gun(That has Rigidbody) it gets that weapons index number.So i can use that number to activate(disabled) weapon. Something like :

    Code (csharp):
    1.  
    2. if(curWeapon == 1)
    3. {
    4. MainWep = WpIndex;
    5. }
    6. if (WpIndex == 1 )
    7. {
    8. Weapon1 = True;
    9. AllOtherWp = false;
    10. }
    11.  
    Or if you know a better way to pick up weapons.
    Something like that i don't really know i just made it.
    I hope it's more understanding

    Thanx for time
     
  5. The3DKnight

    The3DKnight

    Joined:
    Feb 8, 2011
    Posts:
    240