-1

I'm new to working in unity and i don't quite understand how to transfer data between scenes. In my case it's the sprites for my inventory when the player picks up an item. here is the code for the items sprites etc.

public class Items : MonoBehaviour

{

public string ItemName;
public string ItemText;
public int itemInList;
public Sprite itemSprite;
public InventorySlot inventorySlot;


public ItemSprites itemSprites;
public Sprite blueberrySprite;
public Sprite cheesecakeSprite;
public Sprite whitebreadSprite;
public Sprite cookiesSprite;


public void Awake()
{
    //DontDestroyOnLoad(this);
}

public void gettingID()
{
    GameObject Go = new GameObject();
    Go.AddComponent<InventorySlot>();
    inventorySlot = Go.GetComponent<InventorySlot>();

    inventorySlot.itemID = itemInList;

}

public void ItemList() 
{

    // Cheesecake
    if(itemInList == 1)
    {
        ItemName = "Cheesecake";
        ItemText = "A delishious Juicy Cheesecake";
        itemSprite = cheesecakeSprite;
    }
    // White Bread
    if (itemInList == 2) 
    {

        ItemName = "White Bread";
        ItemText = "a basic white loaf that smells amazing";
        itemSprite = whitebreadSprite;

    }
    // Cookies
    if (itemInList == 3) 
    {
        ItemName = "Cookies";
        ItemText = "just plain Chocolatechip cookies, still delishious though";
        itemSprite = cookiesSprite;
    }
    // Blueberries
    if (itemInList == 4) 
    {
        ItemName = "Blueberries";
        ItemText = "Big juicy yummy blueberries!";
        itemSprite = blueberrySprite;
        Debug.Log(ItemText);

    }

}

it works just fine when I'm in the scene where the script exist but when I try to use it in my new scene the image will turn out white(blank). I guess that is because the gameobject is not set anymore in the new scene, but how do i transfer the set gameobject sprite to a new scene? I now that you can make stuff static but if I read correctly then it doesn't work on gameobjects. and Dontdestroyonload doen't seem to work either cus the script is not attached to a gameobject in the scene.

Hamid Yusifli
  • 9,688
  • 2
  • 24
  • 48
Yumaisch
  • 17
  • 4

1 Answers1

0

a) You can make your sprite a static property in your class and you should be able to access it from anywhere in unity. You can make a simple DataHolder class to hold this value and other static values in your Game (It need not be a MonoBehaviour). However, you will need to load them somehow (From resources or AssetBundles) into your static Array

b) Check my Answer here on how to properly persist a GameObject in the Game if you are using DontDestroyOnLoad and have it attached to a GameObject in the scene. This should be a good option to use, considering you already have a MonoBehaviour class. Just attach it to a GameObject and Follow the answer in the link to maintain one correct instance of the Component.

c) Consider using a simple ScriptableObject if you want to save these values across multiple Game Sessions. It is a simple data container that helps you save data Independent of Classes. Follow this link for Unity's Documentation on ScriptableObjects

varunkaustubh
  • 301
  • 1
  • 7