- Home /
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
Just add 1
to a counter every time you destroy
a coin, then add a condition before the level is exited. If you don't know how to do that, you should look up how to code and then come back here :)
Answer by HappyMoo · Jan 05, 2014 at 01:02 PM
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.
Thank you for this. Time for a cup of $$anonymous$$ and lock my self in the office. I will report back with my findings. Thank you
Another thing... put a public variable "nextLevel" on your exits, so you can string your levels together without additional checks in your code
Done. Handy tip. I'll have to sit done with some reference. as i get errors. I'm probably being dumb but is the above c#.
I usually do C#, but seeing you use Javascript, I tried to also write Javascript, but slipped one time.. void should be function. Fixing it above. $$anonymous$$ore errors?
This is cool. I only get two errors now. i think it has something to do with the tag.
Assets/Scripts/CoinReport.js(5,64): BCE0005: $$anonymous$$ identifier: 'Exit'. Assets/Scripts/LoadLevel.js(4,62): BCE0005: $$anonymous$$ identifier: 'Exit'.
$$anonymous$$y Exit object has a tag set to Exit.
Here is my Script which is attached to each Coin...
#pragma strict
function Start()
{
var CoinReport = GameObject.FindWithTag("Exit").GetComponent(Exit);
CoinReport.registerCoin();
}
And here is the LoadLevel Script which is attached to the Exit Object...
#pragma strict
var exitSound : AudioClip;
var CoinReport = GameObject.FindWithTag("Exit").GetComponent(Exit);
CoinReport.coinPickedUp();
var coinsAlive = 0;
function registerCoin()
{
coinsAlive++;
}
function coinPickedUp()
{
coinsAlive--;
}
function OnTriggerEnter (other : Collider)
{
if(other.tag == "Player" && coinsAlive==0)
{
audio.PlayOneShot (exitSound);
yield WaitForSeconds (0.9);
Application.LoadLevel (1);
}
}
Answer by superluigi · Jan 05, 2014 at 09:28 AM
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.
Thanks for the reply. This is very helpful. I will post my script that destroys and adds score in a short while. Got to go and do dad things before I can lock my self in my office for the day.
Hi there again.
This is the script that i attach to my Player which sets the score and destroys the Coins....
#pragma strict
private var score : int = 0;
var guiScore : GUIText;
var coinSound : AudioClip;
function Start () {
guiScore.text = "Score: 0";
}
function OnTriggerEnter( other : Collider ) {
if (other.tag == "Coin") {
audio.PlayOneShot (coinSound);
yield WaitForSeconds (0.3);
score += 5;
Destroy(other.gameObject);
guiScore.text = "Score: " + score;
}
}
And this script is attached to my exit object...
#pragma strict
var coinSound : AudioClip;
function OnTriggerEnter (other : Collider)
{
if(other.tag == "Player")
{
audio.PlayOneShot (coinSound);
yield WaitForSeconds (0.9);
Application.LoadLevel (1);
}
}
I Think i understand what you mean with the use of a boolean. Thank you
Answer by coolfireking2 · Jan 05, 2014 at 09:43 AM
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 :)
Thanks, Joseph
This doesn't answer the question. The question asked how to count not how to "exit"
Answer by OrbitSoft · Jan 05, 2014 at 09:31 AM
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
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613125606im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Cannot touch cloned prefabs 0 Answers
Why isn't this script working? (Beginner in coding) 2 Answers
Can I utilize LoadLevelAdditive for immersive game? 2 Answers
exit level and return to main menu 3 Answers
Destroy vs Don't Destroy 2 Answers