Can somebody help me with an Inventory script? It's already made but it doesn't work.

I have a simple inventory with 2 slots. In the “Inventory” script the slots are transform variables so Unity can get some info (like name) about the specific item that occupies a certain slot and each one has a boolean variable that shows wether the slots are used or not. I have another script called “Items” in which Unity parents the picked up item on a certain event(player presses the key “I” when in range) and changes the slots booelan variable in order to let other items occupy other slots. The problem is it doesn’t work.
Here’s the “Items” script:

  1. var Inventory:Transform;
  2. var player:Transform;
  3. var distance;
  4. var item=true;
  5. var pickingUp=false;
  6. var pickingUpDistance=5;
  7. var Name = “”;
  8. var pickedup=false;
  9. function Update () {
  10. if (item==true) {
  11. distance = Vector3.Distance(player.position, transform.position);
  12. if(distance < pickingUpDistance){
  13. pickingUp = true;
  14. }
  15. else{
  16. pickingUp = false;
  17. }
  18. }
  19. //if the player chooses to pick up item…
  20. if(pickingUp==true){
  21. if(Input.GetKeyDown(KeyCode.E)){
  22. MoveItemToPlayer();
  23. pickedup=true;
  24. }
  25. }
  26. //let’s find a free slot
  27. if(pickedup==true){
  28. if(Inventory.GetComponent(“Inventory”).slot1ocuppied==false){
  29. Inventory.GetComponent(“Inventory”).slot1 = GameObject.Find(Name).transform;
  30. Inventory.GetComponent(“Inventory”).slot1ocuppied=true;
  31. }
  32. else{
  33. if(Inventory.GetComponent(“Inventory”).slot1ocuppied==true){
  34. Inventory.GetComponent(“Inventory”).slot2 = GameObject.Find(Name).transform;
  35. Inventory.GetComponent(“Inventory”).slot2ocuppied=true;
  36. }
  37. }
  38. }
  39. }
  40. function MoveItemToPlayer()
  41. {
  42. transform.collider.isTrigger=true;
  43. transform.renderer.enabled=false;
  44. transform.parent = Inventory;
  45. transform.position = Inventory.transform.position;
  46. transform.rotation = Inventory.transform.rotation
  47. }

And here’s the “Inventory” script

  1. var drawInventory=false;

  2. var slot1:Transform;

  3. var slot2:Transform;

  4. var slot1occupied = false;

  5. var slot2ocuppied = false;

  6. function Update () {

  7. if(Input.GetKeyDown(KeyCode.I)){

  8. drawInventory=!drawInventory;

  9. }

  10. }

  11. function OnGUI(){

  12. if(!drawInventory)

  13. return;

  14. GUI.Button(Rect(100,175,100,50), “Inventory”);

  15. GUI.Box(Rect(100,225,100,25), slot1.GetComponent(“ItemAttributes”).Name);

  16. GUI.Box(Rect(100,250,100,25), slot2.GetComponent(“ItemAttributes”).Name);

  17. }

  18. //the script "ItemAttributes contains only one variable and it is attached to each

  19. //item

It looks like when you pickup an item, it puts it in slot1, but then doesn’t “turn off” picking up, so also puts itself in slot2 next Update and forever. When you pickup something else, it puts itself in slot2 over and over, fighting with object#1.

But look at the variables to check that. When you grab a second item, look at every variable in Inspector for the player and the item. In the script, add a Print/Debug just inside every if that says things like “in pickup, using slot 1.” Keep going until you can see the exact path the program is running through your code. It’s “doing nothing” for a specific reason, you just have to find it.