Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Picking up items and make them not respawn on scene return.

Discussion in 'Physics' started by boffa, Aug 5, 2022.

  1. boffa

    boffa

    Joined:
    Jun 15, 2019
    Posts:
    2
    Hi! Not sure if this is the right part of the forum, but..

    I am very new to Unity, so apologies if this is a noob question. :)

    I have a character that can pick up items in my game, and the items get removed and added to inventory and a list of picked up items , so that´s all great. But if I change the scene and come back to the first one, the items have respawned. And I can´t figure out how to make them stay removed. I have watched several tutorials on persistence and some other stuff, but can´t get it to work.

    Basically what I want is something like this in my levelloader script:

    start()
    {
    for (int i = 0; i < SaveManager.instance.activeSave.pickedUpItems.Count; i++ )
    {
    if (GameObject.Find(SaveManager.instance.activeSave.pickedUpItems.ToString()))
    {
    Destroy(gameObject);
    }
    }
    }

    No idea if this is the right way to do it, but seems like a it would be a simple way to do it? I just want to compare the objects in the scene (and later on if an enemy has been killed, so they don´t respawn) to my picked up items list and destroy them if they have been added to the list so they don´t spawn.

    btw; When I add the items to my list I use: SaveManager.instance.activeSave.pickedUpItems.Add(Item.GetInstanceID());
    so that it saves the instanceID of the object, but can´t figure out how to use that when removing it.
     
    Last edited: Aug 5, 2022
  2. knobblez

    knobblez

    Joined:
    Nov 26, 2017
    Posts:
    223
    You could use a static bool to determine if the object is "pickedup". Probably something like this:
    Code (CSharp):
    1.                 for (int i = 0; i < SaveManager.instance.activeSave.pickedUpItems.Count; i++)
    2.                 {
    3.                     if (SaveManager.instance.activeSave.pickedUpItems[i].GetComponent<ITEMSCRIPT>().pickedUp == true)
    4.                     {
    5.                         Destroy(SaveManager.instance.activeSave.pickedUpItems[i].gameObject);
    6.                     }
    7.                 }
     
  3. boffa

    boffa

    Joined:
    Jun 15, 2019
    Posts:
    2
    Hmm.. not sure if this will work, but:

    for (int i = 0; i < SaveManager.instance.activeSave.pickedUpItems.Count; i++)
    {
    if (SaveManager.instance.activeSave.pickedUpItems.isPickedUp == true)
    {
    Debug.Log("item " .ToString());
    GameObject objectToDestroy = GameObject.Find(SaveManager.instance.activeSave.pickedUpItems.name);
    DestroyImmediate(objectToDestroy, true);
    }
    }

    Problem is I´m getting a "IOException: Sharing violation on path" when I try to enter the scene again. Know I had this before a while back, but can´t remember how to solve it.