Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Collect a coin go to the next level

Discussion in 'Scripting' started by DoraemonCrayon, Mar 10, 2020.

  1. DoraemonCrayon

    DoraemonCrayon

    Joined:
    Mar 10, 2019
    Posts:
    27
    I want a script that, when collecting a certain coins activates a portal to go to the next level, this is the logic;
     
  2. adi7b9

    adi7b9

    Joined:
    Feb 22, 2015
    Posts:
    181
    Code (CSharp):
    1. if (certain coin)
    2. {
    3.   go to next level
    4. }
     
  3. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    You have an OnTriggerEnter somewhere in which you detect whether you collect the coin. When the coin was collected, spawn your portal. When the player touches the portal, load a new scene. Where is your problem?
     
    Last edited: Mar 10, 2020
  4. WallaceT_MFM

    WallaceT_MFM

    Joined:
    Sep 25, 2017
    Posts:
    394
  5. DoraemonCrayon

    DoraemonCrayon

    Joined:
    Mar 10, 2019
    Posts:
    27
    I'll give you an example, I have 10 coins scattered in the game environment, collecting the 10 coins activates a portal that goes to the next level! And only in this way to move to the next level, this is the logic I want (similarly donate collecting a key that will open a door to advance, only I want it with coins)
     
  6. DoraemonCrayon

    DoraemonCrayon

    Joined:
    Mar 10, 2019
    Posts:
    27
  7. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    I basically already told you how to do this. Somewhere in your coin collection code you check for collision with the coins, probably using an OnTriggerEnter. Here, if all coins are collected, spawn a portal. When touching the portal, load a new scene.

    The easiest way to check if all coins are collected is to save the amount of collected coins in a public static (which is globally accessible from everywhere) variable. This is not very object oriented tho, so you can also approach the problem by introducing a sort of CoinCollectionManager class, which is known to all coins. When a coin is collected, it calls some method in the CoinCollectionManager, which increments the amount of collected coins, and then if all coins are collected, spawns the portal. Either of those approaches is fine. I'd personally go with the manager type class and make it a Singleton.

    If you need help with any of the above, feel free to tell us with what specifically.