• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Oct 04, 2022 at 06:05 AM by zahidashahidraju.
avatar image
0
Question by zahidashahidraju · Sep 23, 2022 at 08:11 AM · gameobjectplayerunity 2dgrabprogress

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 = GetComponentsInC$$anonymous$$ldren<ObjectIDController>();
               for(int i = 0; i < giftsGameObjArray.Length; i++)
                  {
                    if(giftsGameObjArray[i].grabbed == true)
                   {
                         giftsGameObjArray[i].gameObject.SetActive(false);
                    }
             }

       }
 }

Trigger Function when player collect gift or coin


    void OnTriggerEnter2D(Collider2D collision)
   {
     
    
         collision.enabled = false;
         cherryCount += 1;
         collision.GetComponent<ObjectIDController>().grabbed = true;
         collision.GetComponent<CircleCollider2D>().enabled=false;
         collision.GetComponent<SpriteRenderer>().enabled = false;
         
         scoreManager.UpdateCherryText(cherryCount);
         
     }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
0

Answer by RobinTollerHovler · Sep 23, 2022 at 01:02 PM

When you reload the scene the old objects will "reset" to their initial state. Persistence can be ac$$anonymous$$eved in a number of ways, depending on what you need, ranging from persistence between scenes to persistence between game sessions. To solve t$$anonymous$$s 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 t$$anonymous$$s 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. :)

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image zahidashahidraju · Sep 26, 2022 at 05:14 AM 0
Share

@RobinTollerHovler It works fine when I reload scene in without quitting game. But I need this when I restart actually continue the game ,

In simple lets say you are playing game and saved you collected some gifts , coins and killed some enemies, now you just quit the game, after cupule of hours or days when you say I wanna continue the game where I quit last time. In this case I wanna hide (or not active) the particle gifts you collected and enemies you killed.

But here

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

this list will reset to initial state. I don't want this

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

242 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to pass a gameobject between scenes 3 Answers

rigidbody.velocity not working on x axis 2 Answers

How can i solve this issue?? i need real help. 1 Answer

Function to assign gameobject components with parameter strings... 1 Answer

How do you change gravity with a press of a button? Unity 2D 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges