• 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 /
avatar image
0
Question by AlexJBoyd · May 29, 2013 at 06:31 PM · arraystaticcollectible

Picking Up Collectibles One Time Between Scenes

So I am working on a project in which the player goes through the level as far as they can before they run out of "Air". Along the way, they can collect different coins for money. When the player exits the level they can go to the store to purchase upgrades. Then they can go back to the same level (if they choose) and keep exploring further.

The problem is, my coins are not static. If I collect the small bunch I have in the begging of the level, then exit, and then return to the level the same coins are there. Because of this, you can just repeat that process a couple dozen times and purchase all the upgrades possible.

So my question is, how do I make sure once I have something collected, and the scene is loaded again that the collect items no longer appear? I know it is something along the lines of a static array, but I am lost. Also, the coins are already places in the 3 levels, they are not placed dynamically.

Thank You

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

· Add your reply
  • Sort: 
avatar image
0

Answer by hiddenspring81 · May 29, 2013 at 06:58 PM

You'll need two components; one component to store which coins you've collected, and a component to store an ID for a given coin instance. This will store you coin ID

 public Coin : MonoBehaviour
 {
   public int CoinId;

   void Reset ()
   {
     // Give each coin an unique ID when added to a GameObject
     CoinId = UnityEngine.Random.Range(0, 10000);
   }
 }

And this is the component that you'll use to store which coins you've already collected

 public CollectedCoins : MonoBehaviour
 {
   List<int> _collectedCoins;
   
   void Awake ()
   {
     // A list of the coins that we've already collected
     _collectedCoints = new List<int>();
     // Make sure that this component isn't destroyed when switching levels
     DontDestroyOnLoad(this);
   }
 
   // Runs whenever we switch scenes
   void OnLevelWasLoaded ()
   {
     var coins = GameObject.FindObjectsOfType(typeof(Coin));
     foreach (var coin in coins)
     {
       // Have we already collected this coin?
       if (_collectedCoins.Contains(coin.CoinId))
       {
         // If we've already collected it, then remove object from the scene
         GameObject.Destroy(coin.gameObject);
       }
     }
   }
 
   public void CollectCoin (Coin coin)
   {
     if (_collectedCoins.Contains(coin.CoinId))
     {
       Debug.LogError("You've already collected " + coin.CoinId);
     }
     else
     {
       _collectedCoins.Add(coin.CoinId);
     }
     GameObject.Destroy(coin.gameObject);
   }
 }

This code hasn't been tested, so let me know if you have any problems. Also, you will need to make sure that each coin has a unique ID, or the system won't work.

Comment
Add comment · 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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

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

14 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

Related Questions

Script effects all gameobjects. 1 Answer

GameObjects static array NullReferenceException 1 Answer

Static Array 2 Answers

Access Array from static void (C#) 2 Answers

Static array with custom class? 1 Answer


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