Search Unity

Problem with GetComponent and Destroy

Discussion in 'Scripting' started by Dentarg, Jan 22, 2014.

  1. Dentarg

    Dentarg

    Joined:
    Nov 2, 2013
    Posts:
    1
    Hello Community,

    First at all I want to say that I like the Unity Engine very much, but now I've got a problem I don't know how to handle it clearly. I tried to find a solution with google but it didn't work.


    My Game I want to make is a top down hack and slash. (I know there is this tutorial with burgzerg, but that couldn't answer my question neither)

    Now I try to create a pick up and inventory system similar to diablo.
    Like: I can see an item on the ground and when I walk over it I would have it in my left hand.

    My idea was:
    The Item has got a lot of information (like which weapon? Or the name of it.. damage...)
    And when I got it. My equipment (weaponLeft) should get all information.
    After that the Object should be destroyed.

    My problem is:

    If I don't destroy the weapon on the floor. Everything works fine. I can shoot or hit and I got all attributes of the weapon.
    But what should I do with the weapon? It should disappear.

    But when I destroy the object. It say that weaponLeft is null. But When I'm debugging
    Code (csharp):
    1. print(weaponLeft.getDamage());
    It will find the value of my picked weapon.

    So that's why I'm confused. What was my mistake?

    The only thing what I thought is, that I can try it with a new instance like
    Code (csharp):
    1. WeaponAttributes newWeapon = new WeaponAttributes(content);
    But that can't be the solution. It's only that weaponLeft is no longer a WeaponAttributes the rest is ok.




    The code of the files:

    Player.Pickup.cs

    Code (csharp):
    1.  
    2.  
    3. public class PlayerPickUp : MonoBehaviour {
    4.  
    5.     private PlayerEquipment equipment;  // all stuff which the player can carry
    6.     private PlayerAttributes attributes;    // attributes like str, hp
    7.  
    8.    void Update() {
    9.         equipment = GetComponent<PlayerEquipment>();
    10.         attributes = GetComponent<PlayerAttributes>();
    11.     }
    12.  
    13.     /**
    14.      * If the player reaches an item on the floor
    15.     */      
    16.  
    17.     void OnTriggerEnter(Collider item) {
    18.  
    19.         if (item.GetComponent<Item>()) {
    20.  
    21.             Item itemAttributes = item.GetComponent<Item>();
    22.             itemAttributes.setOwner(this.gameObject);
    23.             itemAttributes.setId(this.attributes.getId());
    24.            
    25.             switch (itemAttributes.getItemCategory()) {
    26.                
    27.                 case "weapon":
    28.                     equipment.setWeaponLeft(itemAttributes.GetComponent<WeaponAttributes>());
    29.                     break;
    30.                
    31.                 case "gold":
    32.                    
    33.                     break;
    34.    
    35.             }
    36.    
    37.             if (itemAttributes.getIsPickable()) {
    38.                 equipment.setGotNewItem(true);
    39.                 Destroy(item.gameObject);
    40.             }
    41.        
    42.         }
    43.            
    44.     }
    45.  
    46. }
    47.  
    48. Equipment:
    49.  
    50. public class Equipment : MonoBehaviour {
    51.  
    52.    private WeaponAttributes weaponLeft;
    53.   /*** other code
    54.   */
    55.  
    56.  public void setWeaponLeft(WeaponAttributes value) {
    57.     this.weaponLeft = value;
    58.  }
    59.  
    60.  /** other code
    61.  */
    62.  
    63.  
    64.  

    Thank you for your help. If something is not clear pls tell me.

    Greetings
    Dentarg