Player Runs through Object but does not pick up item

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();
}
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().amount < 2)*
{
if(itemName == inventory.slots*.transform.GetComponentInChildren().itemName)*
{
Destroy(gameObject);
inventory.slots*.GetComponent().amount += 1;*
break;
}
else if (inventory.isFull == false)
{
inventory.isFull = true;
Instantiate(itemButton, inventory.slots*.transform, false);*
inventory.slots*.GetComponent().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();

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

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…

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:

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

9633287--1368857--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

9633287--1368857--Untitled design.gif

without watching the tutorial

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

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;
                    }
                }
            }
        }
    }
}

Just tried the reformatting you suggested but still no luck on the collision, the objects do not pick up or add to the inventory.

I didnt suggest reformatting, i pointed out there were logic errors. Without changing them, no it wont.

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

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. :slight_smile:

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.

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

I see what you are referring to now, thank you for your help.

This is now fixed