Search Unity

Question Player Runs through Object but does not pick up item

Discussion in 'Scripting' started by DuncMch, Feb 8, 2024.

  1. DuncMch

    DuncMch

    Joined:
    Sep 28, 2023
    Posts:
    6
    Hi there I have been using the following tutorial:


    I have followed it to the letter but for some reason my player runs through the pickup items and does not collect them.

    Here are the scripts for the pickup function:

    PickUp Script

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class PickUpNew : MonoBehaviour
    {
    private InventoryController inventory;
    public GameObject itemButton;
    public string itemName;
    [System.Obsolete]
    private void Start()
    {
    inventory = FindObjectOfType<InventoryController>();
    }
    private void OnTriggerEnter(Collider other)
    {
    if(other.CompareTag("Player"))
    {
    for (int i = 0; i < inventory.slots.Length; i++)
    {
    if (inventory.isFull == true && inventory.slots.transform.GetComponent<Slots>().amount < 2)
    {
    if(itemName == inventory.slots.transform.GetComponentInChildren<Spawn>().itemName)
    {
    Destroy(gameObject);
    inventory.slots.GetComponent<Slots>().amount += 1;
    break;
    }
    else if (inventory.isFull == false)
    {
    inventory.isFull = true;
    Instantiate(itemButton, inventory.slots.transform, false);
    inventory.slots.GetComponent<Slots>().amount += 1;
    Destroy(gameObject);
    break;
    }
    }
    }
    }
    }
    }

    Slots for Inventory System:
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using TMPro;
    public class Slots : MonoBehaviour
    {
    private InventoryController inventory;
    public int i;
    public TextMeshProUGUI amountText;
    public int amount;
    // Start is called before the first frame update
    [System.Obsolete]
    void Start()
    {
    inventory = FindObjectOfType<InventoryController>();

    }
    // Update is called once per frame
    void Update()
    {
    amountText.text = amount.ToString();
    if(amount > 1)
    {
    transform.GetChild(0).GetComponent<TextMeshProUGUI>().enabled = true;
    }
    else
    {
    transform.GetChild(0).GetComponent<TextMeshProUGUI>().enabled = false;
    }
    if(transform.childCount == 2)
    {
    inventory.isFull = false;
    }
    }
    public void DropItem()
    {
    if(amount > 1)
    {
    amount -= 1;
    transform.GetComponentInChildren<Spawn>().SpawnDroppedItem();
    }
    else
    {
    amount -= 1;
    GameObject.Destroy(transform.GetComponentInChildren<Spawn>().gameObject);
    transform.GetComponentInChildren<Spawn>().SpawnDroppedItem();
    }
    }
    }

    Spawn Object back on Drop

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class Spawn : MonoBehaviour
    {
    public GameObject itemPrefab;
    private Transform player;
    public string itemName;
    // Start is called before the first frame update
    void Start()
    {
    player = GameObject.FindGameObjectWithTag("Player").transform;
    }
    public void SpawnDroppedItem()
    {
    Vector3 playerposition = new Vector3(player.position.x, player.position.y, player.position.z + 4);
    Instantiate(itemPrefab, playerposition, Quaternion.identity);
    }
    }

    Inventory Controller

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class InventoryController : MonoBehaviour
    {
    public bool[] isFull;
    public GameObject[] slots;
    }


    I have tried Debug.Log but still can not find the error in the code but I feel it has something to do with the player tag.

    Would someone be able to point me in the right direction?
     
  2. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    1,696
    More people will be inclined to read your post if you used code tags, so your code was formatted (assuming you formatted your code well in your editor)

    There are a lot of stock answers as to why something hasnt happened but few will watch an entire tutorial, so you could also help yourself by showing some of your settings on the items in question eg your players components, as well as the settings on the object to pickup (and only stuff relevant to picking up, such as colliders, rigidbodys, is this script on one of them, which one, layers, collision matrix even.. see theres a lot it could be, and we cant see... Such as, we dont need to know about spawn on drop, you cant pick it up, so dropping it is irrelevant to the question..
     
  3. DuncMch

    DuncMch

    Joined:
    Sep 28, 2023
    Posts:
    6
    That's fair enough I will include some screenshots to show where everything is connected up.

    The PickUpNew Script has been attached to a simple cube which contains a Box Collider and a Trigger for this collider which based on the script should pick up when the item tagged Player runs through it which you can see my player is already tagged in the other image here:

    upload_2024-2-9_8-42-20.png

    upload_2024-2-9_8-43-35.png

    However when I run into the object they do not destroy or update the canvas for inventory on the right.

    Untitled design.gif
    Any help would be greatly appreciated, I am a first time poster so if you need more information please let me know
     

    Attached Files:

  4. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    1,696
    without watching the tutorial

    Code (CSharp):
    1. if (inventory.isFull == true && inventory.slots.transform.GetComponent<Slots>().amount < 2)
    this line makes no sense to me. why would you keep testing the same slot? plus you only actually seem to do it if the inventory is full, yours is empty.

    Your else for inventory not full, is inside full so that code never runs, ever

    hence formatting counts - see how much more readable this is now here? you can also see immediately that the if isfull == false is inside the if true. (PS no code was changed in this process)

    look
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class PickUpNew : MonoBehaviour
    5. {
    6.     private InventoryController inventory;
    7.     public GameObject itemButton;
    8.     public string itemName;
    9.     [System.Obsolete]
    10.     private void Start()
    11.     {
    12.         inventory = FindObjectOfType<InventoryController>();
    13.     }
    14.     private void OnTriggerEnter(Collider other)
    15.     {
    16.         if(other.CompareTag("Player"))
    17.         {
    18.             for (int i = 0; i < inventory.slots.Length; i++)
    19.             {
    20.                 if (inventory.isFull == true && inventory.slots.transform.GetComponent<Slots>().amount < 2)
    21.                 {
    22.                     if(itemName == inventory.slots.transform.GetComponentInChildren<Spawn>().itemName)
    23.                     {
    24.                         Destroy(gameObject);
    25.                         inventory.slots.GetComponent<Slots>().amount += 1;
    26.                         break;
    27.                     }
    28.                     else if (inventory.isFull == false)
    29.                     {
    30.                         inventory.isFull = true;
    31.                         Instantiate(itemButton, inventory.slots.transform, false);
    32.                         inventory.slots.GetComponent<Slots>().amount += 1;
    33.                         Destroy(gameObject);
    34.                         break;
    35.                     }
    36.                 }
    37.             }
    38.         }
    39.     }
    40. }
     
  5. DuncMch

    DuncMch

    Joined:
    Sep 28, 2023
    Posts:
    6
    Just tried the reformatting you suggested but still no luck on the collision, the objects do not pick up or add to the inventory.
     
  6. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    1,696
    I didnt suggest reformatting, i pointed out there were logic errors. Without changing them, no it wont.
     
  7. DuncMch

    DuncMch

    Joined:
    Sep 28, 2023
    Posts:
    6
    Sorry I'm still having problems with understanding the logic errors, are you saying the else if needs to be moved or am i calling the wrong method
     
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,409
    Have you tried to debug your code? Pasting code for others to debug in their heads isn't going to work well in the long run. :)

    You can place Debug.Log and attach a debugger to step through the logic. Maybe you have already but you should do this before asking on the forums TBH.
     
  9. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    1,696
    then reread what i said looking at the formatted code.

    I shall use some bigger letters

    YOUR CODE WONT RUN BECAUSE IT ONLY DOES ANYTHING IF YOUR INVENTORY IS FULL

    Now reread what i wrote last time as to why
     
  10. DuncMch

    DuncMch

    Joined:
    Sep 28, 2023
    Posts:
    6
    I see what you are referring to now, thank you for your help.
     
  11. DuncMch

    DuncMch

    Joined:
    Sep 28, 2023
    Posts:
    6
    This is now fixed