- Home /
Question by
vilemuse · Feb 08, 2021 at 03:27 AM ·
item pickup
Item is spawning next to inventory slot instead of inside of the inventory slot
When I pick up an item, the item spawns directly next to my inventory slot instead of inside of the inventory box. Any idea as to what I am doing wrong?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ItemPickUp : MonoBehaviour
{
private Inventory inventory;
public GameObject itemButton;
private void Start()
{
inventory = GameObject.FindGameObjectWithTag("Player").GetComponent<Inventory>();
}
private void OnTriggerEnter2D(Collider2D other)
{
if(other.CompareTag("Player"))
{
for (int i = 0; i < inventory.slots.Length; i++)
{
if (inventory.isFull[i] == false)
{
inventory.isFull[i] = true;
Instantiate(itemButton, inventory.slots[i].transform, false);
//Instantiate(itemButton, inventory.slots[i].transform, false);
//Instantiate(newItem, Slots[0].position, Slots[0].rotation);
Destroy(gameObject);
break;
}
}
}
}
}
inventory.png
(29.6 kB)
Comment
Answer by logicandchaos · Feb 08, 2021 at 02:21 PM
Set the position of the new gameObject to where you want it to be. You can set it in the instantiate method, or make a reference for it and set it after instantiate.
Your answer
![](https://koobas.hobune.stream/wayback/20220613024755im_/https://answers.unity.com/themes/thub/images/avi.jpg)