I have a simple system for generating an inventory screen using basic prefabs, however, depending on where the inventory item is instantiated the gameobject shifts like the image below. (top was in the inventory before running and the second during play.)
enter image description here
Below here is the script incharge of instatiating everything. I cannot for the life of me figure out what is different
using System.Collections;
using System.Collections.Generic;
using JetBrains.Annotations;
using Unity.Mathematics;
using UnityEngine;
using TMPro;
using UnityEngine.UI;
using Unity.VisualScripting;
public class DisplayInvScript : MonoBehaviour
{
public int X_SPACE_BETWEEN_ITEMS, NUMBER_OF_COLUMNS, Y_SPACE_BETWEEN_ITEMS;
public GameObject prefab;
public Inventory inventory;
Dictionary< InventorySlot, GameObject> itemsDisplayed = new Dictionary<InventorySlot, GameObject>();
public int x_START, Y_START;
void Start()
{
for (int i = 0; i < inventory.Container.Count; i++){
var obj = Instantiate(prefab, Vector3.zero, quaternion.identity, transform);
obj.GetComponent<RectTransform>().localPosition = GetPosition(i);
Transform[] textlist = obj.GetComponentsInChildren<Transform>();
foreach (var thing in textlist){
if (thing.name == "Item Name")
{
thing.GetComponent<TextMeshProUGUI>().text = inventory.Container[i].item.idName;
}
else if (thing.name == "Item Amt")
{
thing.GetComponent<TextMeshProUGUI>().text = inventory.Container[i].amount.ToString();
}
else if (thing.name == "Item Type")
{
thing.GetComponent<TextMeshProUGUI>().text = inventory.Container[i].item.type.ToString();
} else { }
}
itemsDisplayed.Add(inventory.Container[i],obj);
}
}
void Update()
{
for (int i = 0; i < inventory.Container.Count; i++){
if(itemsDisplayed.ContainsKey(inventory.Container[i])){
Transform[] testlist = itemsDisplayed[inventory.Container[i]].GetComponentsInChildren<Transform>();
foreach (var thing in testlist){
if (thing.name == "Item Amt")
{
thing.GetComponent<TextMeshProUGUI>().text = inventory.Container[i].amount.ToString();
}
else { }
}
}
else {
var obj = Instantiate(prefab, Vector3.zero, quaternion.identity, transform);
obj.GetComponent<RectTransform>().localPosition = GetPosition(i);
Transform[] textlist = obj.GetComponentsInChildren<Transform>();
foreach (var thing in textlist){
if (thing.name == "Item Name")
{
thing.GetComponent<TextMeshProUGUI>().text = inventory.Container[i].item.idName;
}
else if (thing.name == "Item Amt")
{
thing.GetComponent<TextMeshProUGUI>().text = inventory.Container[i].amount.ToString();
}
else if (thing.name == "Item Type")
{
thing.GetComponent<TextMeshProUGUI>().text = inventory.Container[i].item.type.ToString();
} else { }
}
itemsDisplayed.Add(inventory.Container[i],obj);
}
}
}
public Vector3 GetPosition(int i){
return new Vector3(x_START + (X_SPACE_BETWEEN_ITEMS * (i % NUMBER_OF_COLUMNS)),Y_START + (-Y_SPACE_BETWEEN_ITEMS*(i/NUMBER_OF_COLUMNS)),0f);
}
}
Tried instantiating it differently and even changing the pivot of the object but nothing seems to work.
joe delderfield is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.