Hi all my friends!
In these days I have seen many tutorials to create an inventory. I found everything very difficult to assimilate, so today I proceeded to a solution “made at home”. I have a player colliding with a box and a canvas slot appears with an attached image. It collides with another cube and the image disappears.
Create a scene with two cube, a player and a Canvas with slot Image…
Script attach to slot image (canvas image):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Slot : MonoBehaviour
{
public GameObj gameobj;
private Image im;
void Start ()
{
im = GetComponent<Image>();
im.enabled = false;
}
public void GetImageFromGameObj()
{
im.enabled = true;
gameobj.imag
im.sprite = gameobj.imag;
}
public void RemoveImageFromGameObj()
{
im.enabled = false;
im.sprite = null;
}
}
Script Cube AddImage:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameObj : MonoBehaviour {
public Sprite imag;
public Slot slot;
void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
slot.GetComponent<Slot>().GetImageFromGameObj();
}
}
}
Script Cube Remove Image:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameObjRemove : MonoBehaviour
{
public Slot slot;
void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
slot.GetComponent<Slot>().RemoveImageFromGameObj();
}
}
}
So far so good, but the image is created in the slot that I choose obviously. Now I would like to create an array of slots in the gui, where to insert images in order of appearance (grab/sort) how to do ?. Frankly, everything is difficult for me to understand …
The idea is to take a key and have your image in the first slot, take a book and have your image in the second slot, etc. (this obviously also when the icons are deleted)…
Could you help me create something really simple and effective?
Thanks!!
