When all Coins are collected, an exit level object appears.

Hi all,

I’m making a simple platform game for my son and so far I’ve managed to solve most problems by reading forums etc etc.

I’m my game the player has to collect coin like objects then head over to the exit object where the player progresses to the next level.

So far everything works. Coins destroy when the player touches them and updates the score, player can exit the level when they touch the exit object. But I’ve found when I ask my son to test the game, some times he skips collecting the coins and goes for the exit object. Thus making the game to simple and short.

My question to all is how would I go about implementing a system where all the coins on the particular level must be collected in order for the exit object to appear in the level in a given place.

Any help to point me in the right direction in regards to reference material or web links, or snippets of script would be great.

Many thanks in advance

There are too many different ways to go about doing this. For example you can add a boolean to your exit object and call it “on” or “activate” or whatever you want. The boolean is of course set to start as false and you can code it to where it becomes true when the score = the total amount of coins. If you tell me how you are managing your score I can help you write the code, as it is I don’t know if you’re handling the score through your character script, through a scene script, etc.

You’ll need a general script where you’ll hold the collected coins count, let’s say it will be the script attached to your exit gameObj(I will call it ExitObj).

So, ExitObj needs to count how many coins the player collected:

public class Coin : MonoBehaviour
{
   void Awake()
   {
   ExitObj.coinsInLevel++;
   }
}

By adding this function to your coin script you will keep count of how many coins are in the level. Before destroying the coin you need to add collectedCoins by 1 like this:

ExitObj.collectedCoins++;
Destroy(this.gameObject);

And add this to your ExitObj.

public class ExitObj : MonoBehaviour
{
    public static int coinsInLevel;
    public static int collectedCoins;

    void Update()
    {
      if (collectedCoins == coinsInLevel && Input.GetKeyDown("somekey"))
      {
      // exit level
      }
    }
}

Hi,

I have a few suggestions.

You could count your coins manually and then add a check for the player score in the exit object, but if you have multiple levels, you would need to keep track of a running sum etc… there’s a better way:

Have every coin in the level report itself to the exit object

function Start()
{
  var theExit = GameObject.FindWithTag("Exit").GetComponent(Exit);
  theExit.registerCoin();
}

and also report to the Exit when you pickup a coin …

var theExit = GameObject.FindWithTag("Exit").GetComponent(Exit);
theExit.coinPickedUp();

Then in the exit, keep track of how many coins registered and got picked up

var coinsAlive = 0;
function registerCoin()
{
  coinsAlive++;
}
function coinPickedUp()
{
  coinsAlive--;
}

and check for that value before you leave the level

function OnTriggerEnter (other : Collider)
{
    if(other.tag == "Player" && coinsAlive==0)
    {
       audio.PlayOneShot (coinSound);
       yield WaitForSeconds (0.9);
       Application.LoadLevel (1);
 
    }
}

To make it visually understandable, I would recommend that the Exit changes appearance. Hide some or all visual components in Start() and in coinPickup() check if you reach 0 and if so, make it visible, play your exit activation sound etc.

Regarding the level design, I would recommend to not make all coins needed to pick up, but have normal coins that are optional and some master coins(and only those report to exit) in another color that you have to pick up. Then place the master coins in a way that leads you to most of the normal coins, but still allows you to skip some normal coins.

Also, let your son only playtest the newest level and not too often, then when the game is done, he still finds every level interesting and hasn’t played every one of them - especially the first ones - uncountable times.

Hello,

You will want to use Instantiate e.g.

var prefab : Transform;

function Update(){

if(coinsCollectedVar == 50) { //How many coins you need

Instantiate (prefab, Vector3(0,0,0)); //position of where the object is spawned

}

}

Then set the prefab var to your exit game object.

Please note this code is untested but i think it will work :slight_smile:

Thanks,
Joseph