Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Disable Coin after first collision

Hello fellow programmers!

Me and a friend is working on a little game for us to learn how to do stuff.

We have a few different scenes (called S1, S2 from now on) and also a Hub where you can access all the other scenes.

In every scene we have a few coins to collect. When you collect a coin we want to disable that coin for that whole playsession, meaning that when we go back to that level that coin will no longer be there for the player to collect. How can we do this?

We have tried doing this:

gameObject.GetComponent<SpriteRenderer>().enabled = false;

But it doesn't work, the Sprite still renders when the player enters the same level again. Anyone got any suggestions? Also, PLEASE don't post the solution straight away, drop a hint instead so we get the learning from it.

Best Regards, O and R

Answers

  • What do you mean by enter the same lvl again.

    Does it changes the scene.if yes then it wont work because when you change scenes all the settings goes to their default state

  • After a level is complete, the player goes back to the Hub, where they then can enter any level they like. So yes, it changes scene after this.

    So there is no way we can get around that somehow?

  • One thing you can do.use player prefs like if you collect coin change the player pref string to true from false.


    Then when you start the scene just check for that player pref string if it is true than make the coin visible or make the coin disappear.

  • Thank you, we'll try this and get back to you know we know if we got it working or not! :)

  • One way you can track the information is to have a root scene that holds all your game tracking logic. This it where you can store and pull into other scenes the information stored in this root scene (such as which coins should be spawning for that level). From there, you can use the Additive Scene functionality to load in each other scene you want to transition to.

    https://docs.unity3d.com/ScriptReference/SceneManagement.LoadSceneMode.Additive.html

    Another potential way is to look into using Serialization to store this information onto the computer's long term memory storage so that it can be recalled when rebuilding the scene later. There are multiple ways to go about storing this data, both natively from Unity and natively from the C# language itself.

    https://docs.unity3d.com/Manual/script-Serialization.html

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/serialization/

    I'll leave it to you all to analyze the benefits and downsides of each of these approaches. Good luck mates!

  • tomfortomfor Member
    edited May 7

    I don't quite understand your question. Maybe you should leave the option to take a coin every time after you pass a level? 

Sign In or Register to comment.