- Home /
How can I transfer the item's image in inventory UI to the next scene?,How can I keep and transfer the image of items in the inventory in different scenes?
I get stunk in this point for 3 weeks already. The situation is that it can only transfer my inventory canvas UI to the next scene without the item's image in the inventory.
I put below script in the beginning of my inventory script. The InitUI will be called when change scene.
public static Inventory instance;
public GameObject UIOBJ;
public Text PickedUpText;
public GameObject[] inventory = new GameObject[4];
public Button[] InventoryButtons = new Button[4];
void Awake()
{
if (instance == null)
{
instance = this;
DontDestroyOnLoad(this);
name = "theFirstGameManager";
}
else if (this != instance)
{
string sceneName = SceneManager.GetActiveScene().name;
Debug.Log("Delete the " + name + " of " + sceneName);
Destroy(gameObject);
}
}
public void InitUI()
{
Instantiate(UIOBJ);
}
The image of item is displayed by the below script.
public void Update()
{
for (int i = 0; i < inventory.Length; i++)
{
if (inventory[i] != null)
{
InventoryButtons[i].image.overrideSprite = inventory[i].GetComponent<SpriteRenderer>().sprite;
//Debug.Log(inventory[i].name);
}
}
}
public void AddItem(GameObject item)
{
DisableText();
bool itemAdded = false;
//Find the first empty slot in the inventory
for (int i = 0; i < inventory.Length; i++)
{
if (inventory[i] == null)
{
inventory[i] = item;
InventoryButtons[i].image.overrideSprite = item.GetComponent<SpriteRenderer>().sprite; //Update UI
// item add feedback
Debug.Log(item.name + " was added");
PickedUpText.gameObject.SetActive(true);
PickedUpText.text = "You have picked up a " + item.name + ".";
Invoke("DisableText", 1f);
itemAdded = true;
item.SendMessage("DoInteraction");
break;
}
}
I turned the whole UI into prefab. And drag it into UIOBJ.
But for the Inventory Buttons part, as I cannot drag the buttons from the UI prefab, I drag the button from scene 1 hierarchy. (I think this is the problem!)
I have added DontDestroyOnLoad(this.gameObject); on my item.
As a result, the Inventory UI can transfer to the next scene, the items in inventory can transfer to the next scene, but without any item's image showing up in the inventory UI.
So what should I do? Can someone please help? I really need this for my project. Thank you.
Your answer
Follow this Question
Related Questions
Inventory AddItem help 1 Answer
[C#]Inventory script help. 3 Answers