How to make a variable static but for only one of the instant

So, i have 3 coins, but i want to know if each coin is collected and i want to know that in the next scene as well whether that coin is collected or not. If i make the static bool collected; then it would be for all the coins and not just that one. how can i fix this so that each coin is different while i can pass it into the next scene?

Create a static list that contains info on each coin collected? Simply add a coin to the list once it’s collected and in the next scene check if the coin is in that list.

1 Like

Yeah, you’ll need to save this information in a different way. As was suggested above, you may want to save it as a (potentially static) list of bools instead. The thing about ‘static’ is that its whole purpose it to bind a variable to the type itself, instead of its instances. This means that it only exists once, which makes it easily accessable, and also potentially saves some memory. Since it only exists once in memory, however, you cannot have something ‘static per instance’.

Being static per instance is a contradiction, thus effectively cancelling out the term ‘static’, and actually, what we end with is a simple public variable. Think about it, if you want to access something from each instance, then you’ll need the reference to the instance anyways, and if you got that, then you can access anything that is public.
So instead of Coin.IsCollected, you’d simply go through coin1.IsCollected, coin2.IsCollected, …, coinN.IsCollected.

What you really want is global access to this information. As was suggested above, i’d probably just use a list and then either directly save the coins in there, or just their boolean values. This list can either be static, or contained in some persistant (DontDestroyOnLoad) singleton GameLogicManager type of class, or something along those lines.
I’d personally just save the references to those coins in a list, even if you’ll later have to iterate it to look up their “IsCollected” values. If you save just the IsCollected values in a list, you will have to update this list whenever a coin is collected. Depending on the implementation this would likely result in one of many bad design approaches.

Hope this helps!

1 Like

Thank You! I never known there was a something as a static list, had to searched it up but it seems i kinda get it now thanks.

Thanks this information will definitely help me in the long-term, so you’re saying i should create a “static list coins” that is globally accessible and I would just add each of the coins into it. If i were to add it through the coin itself, then it would be “coins.Add(this.gameObject)” I believe. because i have the coins to check when they get hit by the player or is a better design to have the player to check which coin they hit and add that coin to the list?

Well… having that static list of GameObject’s, but then swapping scenes, those GameObjects will be destroyed.

So now you’ll have a list of dead objects. All that remains is the C# side wrapper to the Unity side GameObject which no longer exists (it’s a C# object, but when you check == null it’ll say true).

Not sure what use these GameObjects would be.

If you just need the count… have an int and save a count of how many coins were collected.

…

Personally at the end of the day having random statics around just to carry data over between scenes is a code smell IMO.

Rather instead, what are you attempting to do? What are these coins? What about these coins do you need to know in the next scene?

3 Likes

This. If you only need to know how many coins where collected (or even which ones), then save this data before leaving the scene. If, however, you want to keep the objects, you could do that aswell - for example by making their parent object DontDestroyOnLoad. Not sure if this is what you want tho. I personally would process the data before exiting the scene, then save the data in a compact way.

I imagine you have some levels that the player can play, each containing 3 stars and you want the user to see how many or which stars he already collected for each level in the main menu? For a system like this, you will probably have some place where you save data between sessions and load it again from. So for each level you could simply save the numer of stars, or the order (array of bools), or even bigger structs that could also contain the time at which each star was collected, or whatever. In this situation i wouldnt bind this information to the Coin class, as it would rather belong in your overlying LevelManager or DataManager, or something along those lines.

In case i completely missed what you want to achieve, or you are still confused about the answers:
If you tried to explain what (and why) you want in a bite more detail, we could be a bit more precide about what is the optimal approach for your situation. As it is right now, there are still a lot of reasons for why you may need this, so it’s a bit hard to be very specific when answering.

1 Like

My current game has many, many objects which the player can collect, spread out over scenes they can freely move between, and we need to know which ones have been collected for a variety of reasons.

Our solution to this is to give every collectable item a unique ID, and keep a list of the IDs of objects which have been collected. If all you need to know is which objects have been collected then that’s the guts of what you need right there.