So here is my issue, I am trying to create an inventory system by myself and I want to instantiate a image of an item my character has obtained directly to my inventory UI, a prefab that is connected to an ScriptableObj, but everytime the issue in the title shows up. It works, the image of my item shows up in my inventory, it loops excessively, but I can fix the looping. Nothing in the scene is ever destroyed, I never call the function Destroy(); and I used the ‘if (this != null)’ as required, and yet still the same issue. I just want to know what is wrong here.
here the inventory code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Inventory : MonoBehaviour
{
public Itens[] itens;
public InventorySlot[] inventorySlot;
public int itemNum = 0;
public GameObject inventoryItemImage;
public void AddItemToSlot ()
{
foreach (InventorySlot slot in inventorySlot)
{
if (slot.hasItem == false)
{
if (slot.itemSlot != null)
{
foreach (Itens item in itens)
{
if (item != null)
{
inventoryItemImage = Instantiate(item.itemImage, slot.itemSlot);
}
}
}
}
}
}
void Update()
{
DontDestroyOnLoad(this.gameObject);
foreach (Itens item in itens)
{
if (item != null)
{
AddItemToSlot();
}
}
}
}
[System.Serializable]
public class InventorySlot
{
public RectTransform itemSlot;
public bool hasItem;
}
here the item collection code (this code also does other things, like check if a character is colliding, check for a mouse click, it animates the player as if it’s collecting an item, things related to the idea of collecting an item)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class CharInteraction : MonoBehaviour
{
bool isInRange = false;
bool clicked = false;
bool collected = false;
public GameObject inventory;
public Itens itemID;
public GameObject parent;
public bool animate = false;
public bool disapear = false;
public UnityEvent interact;
GameObject playerStop;
Animator playerInteraction;
Vector3 mousePosition;
void Start()
{
Physics.queriesHitTriggers = true;
inventory = GameObject.FindWithTag("Inventory");
}
void IsClicking ()
{
mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
if (mousePosition.x < parent.transform.position.x +2 && mousePosition.x > parent.transform.position.x - 2 && Input.GetMouseButtonDown(0))
{
clicked = true;
}
}
void Update()
{
IsClicking();
StartCoroutine(InteractionAnimation());
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.CompareTag("Player"))
{
isInRange = true;
playerStop = collision.gameObject;
playerInteraction = collision.GetComponent<Animator>();
}
}
private void OnTriggerExit2D(Collider2D collision)
{
if (collision.gameObject.CompareTag("Player"))
{
isInRange = false;
}
}
void hasBeenCollected ()
{
if (collected == false)
{
inventory.GetComponent<Inventory>().itens[0] = itemID;
collected = true;
}
}
IEnumerator InteractionAnimation ()
{
if (clicked == true && isInRange == true)
{
var stop = playerStop.GetComponent<CharacterMovement>();
stop.enabled = false;
if (animate == true)
{
playerInteraction.SetBool("IsInteracting", true);
yield return new WaitForSeconds(1);
}
interact.Invoke();
if (collected == false)
{
hasBeenCollected();
}
clicked = false;
if (animate == true)
{
playerInteraction.SetBool("IsInteracting", false);
}
stop.enabled = true;
if (disapear == true)
{
parent.SetActive(false);
}
}
}
}
here is my scriptable obj
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
[CreateAssetMenu(fileName = "Item", menuName = "ScriptableObjects/Item", order = 1)]
public class Itens : ScriptableObject
{
public string itemName;
public string itemDescription;
public GameObject itemGameObject;
public GameObject itemImage;
}