Grabbed object not display after reloading scene

I want to not display gifts once collected by the player after reloading the scene.
but when the scene is reloaded it actives all gifts including already grabbed ones.
Any help?

       .

 public class ObjectIDController : MonoBehaviour
{
      public  int id;
       public bool grabbed;

}
 public class GrabbedItemsActive : MonoBehaviour
 {
         public ObjectIDController[] giftsGameObjArray;
        //public GameObject giftsContainer;

    void Start()
     {
           giftsGameObjArray = GetComponentsInChildren<ObjectIDController>();
              for(int i = 0; i < giftsGameObjArray.Length; i++)
                 {
                   if(giftsGameObjArray*.grabbed == true)*

{
giftsGameObjArray*.gameObject.SetActive(false);*
}
}
}
}

Trigger Function when player collect gift or coin

void OnTriggerEnter2D(Collider2D collision)
{

collision.enabled = false;
cherryCount += 1;
collision.GetComponent().grabbed = true;
collision.GetComponent().enabled=false;
collision.GetComponent().enabled = false;

scoreManager.UpdateCherryText(cherryCount);

}

When you reload the scene the old objects will “reset” to their initial state. Persistence can be achieved in a number of ways, depending on what you need, ranging from persistence between scenes to persistence between game sessions. To solve this particular issue where you want to have the object not respawn between scene loads, you could perhaps use a static list saving all collected objects and when the scene loads, you can disable the objects that are present in this list. Static members are not dependent on instances and will persist between scenes. You could put it inside your ObjectIDController:

public class ObjectIDController : MonoBehaviour {
     public int id;
     public bool grabbed;

     private static List<int> collectedObjects = new List<int>();

     private void OnEnable() {
          if(collectedObjects.Contains(id)) {
               //Disable object
          }
     }

     public void Grab() {
          objects.Add(id);
          grabbed = true;
     }
}

And in your trigger you could change to:

void OnTriggerEnter2D(Collider2D collision) {
    collision.enabled = false;
    cherryCount += 1;
    collision.GetComponent<ObjectIDController>().Grab();
    collision.GetComponent<CircleCollider2D>().enabled=false;
    collision.GetComponent<SpriteRenderer>().enabled = false;
    scoreManager.UpdateCherryText(cherryCount);
}

I’m unable to test it atm so tell me if you get issues and I could update it. :slight_smile: