Inventory code creation problem

I used this code to create inventory, but when I run the game it puts it all in one square. For example: if I take two hats it puts me both in the same square in stock, by the way it is from two scripts one for the item being collected and one for the general inventory system that is on an object with a player tag:

The script for the item

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class pickup : MonoBehaviour
{
    public InventroyUI inventroy;
    public GameObject itemButton;

    void Start()
    {
        inventroy = GameObject.FindGameObjectWithTag("Player").GetComponent<InventroyUI>();
    }

   
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if(collision.CompareTag( "Player")) {
            for (int i = 0; i < inventroy.solts.Length; i++)
            {
                if (inventroy.IsFull[i] == false)
                {

                    inventroy.IsFull[i] = true;
                    Instantiate(itemButton, inventroy.solts[i].transform,false);

                    Destroy(gameObject);

                    break;
                }
            }

        }


    }

}

The general inventory system script:

using System.Collections;

Please reply me quickly it's really important!
using System.Collections.Generic;
using UnityEngine;

public class InventroyUI : MonoBehaviour
{
    public bool[] IsFull;
    public GameObject[] solts;
    private void Update()
    {
        solts = GameObject.FindGameObjectsWithTag("InventroySolt");
       
    }
}

I don’t see anything in your code that would actually set the position of the inventory item. Maybe you want something like this at line 25 in your first script:

var newItem = Instantiate(itemButton, inventroy.solts[i].transform,false);
newItem.transform.localPosition = vector3.zero;
                    Instantiate(itemButton, inventroy.solts[i].transform,false);

this

You should consider slowing down a bit and spelling things correctly, such as “slots” and “inventory.”

This may also lead you to a more considered approach to what you’re doing and reduce your overall error rate.

The above code does not necessarily position the object. It sets its parent concurrently with instantiating it. This is critical for UI objects of course, but I have never supplied that third world position argument. Have you tried without the third bool argument? That’s how I learned to instantiate UI things at runtime.

As for the position being wrong, in order gain more insight into your problem, a lot of things can drive positions of UI objects, such as any layout components in the scene.

To isolate what might be going on, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • what are the positions of the slots? Are they all the same?
  • is this code even running? which parts are running? how often does it run?
  • what are the values of the variables involved? Are they initialized?

Knowing this information will help you reason about the behavior you are seeing.

1 Like

@trmhtk2_unity

You didn’t mention it, and I don’t see anything in your code…

Are you using some GameObjects for your UI?

Or are you actually using Unity’s UGUI for UI (Canvas + RectTransforms, Images, Buttons etc)?

this not work for me

I use some GameObjects