collect 10 coins load next level...script not working

private var coinsBegin : int; private var coinsCurrent : int; var levelToLoad:int; //level to load private var coinToNext:int=10; //coin necessary to open next level

function OnGUI() { coinsCurrent = 0; var coinObjects = FindObjectsOfType( GameObject ); for ( var coinObject : GameObject in coinObjects ) { if ( coinObject.name == "coin" ) coinsCurrent++; }

GUI.Label( Rect( 0, 0, 320, 20 ), "Collected " + ( coinsBegin - coinsCurrent ) + " of " + coinsBegin + " coins!" ); }

function Start() { var coinObjects = FindObjectsOfType( GameObject ); for ( var coinObject : GameObject in coinObjects ) { if ( coinObject.name == "coin" ) coinsBegin++; } }

function Update() {

if (coinsCurrent == coinToNext ) { Application.LoadLevel (2); } }

stuck on this all day!! I am trying to modify a coin counter script so that after 10 coins collected, next level will load.

currently, script immediately loads next level when you press play to test, does not wait for you to collect coins...

what am i doing wrong?? thanks for you help...

it's doesn't works because start function get the value of coins in our levels and setting up the value.you nedd to change it whit is colliding in coins.

function Start()
{
var coinObjects = FindObjectsOfType( GameObject );
for ( var coinObject : GameObject in coinObjects )
{
if ( coinObject.name == "coin" )
coinsBegin++;
}
}

replace this function with collision function(see it on the reference manual). i suggest it because our start function automatically add value to the coins when you starting the game,and it's not correct.

function OnCollisionEnter(hit : Collision) 
{
    if (hit.transform.tag == "Coins") { // remember assign Coins tag to our coins game object

//altrnate try it " if (hit.GameObject.name == "coin")  "  but im' not sure it works.
    coinsBegin++;
    } 
}

You don't seem to understand what your code is doing.

OnGUI can be called multiple times per frame. You've got the same code in OnGUI() counting "current coins" as you have in Start(), counting "coinsbegin".

When the script is first run:

  1. Start() will count 'coinsBegin' as 100 (or however many you have).
  2. Then OnGUI() will perform exactly the same count and find the same number.
  3. Then Update() will compare coinsCurrent (the total coin count) with 'coinToNext'

This means that as soon as you press to play, at the end of the first frame, the game will have counted all the coins twice, and checked that coinsCurrent is equivalent to coinsToNext, and LoadLevel 2.


Now to fix it:

The Script Reference page for FindObjectsOfType says:

Please note that this function is very slow. It is not recommended to use this function every frame.

You definitely don't want to be using it in OnGUI, as it could be called multiple times per frame.

Instead, you should have a script on the coins, so every time you collect one (collide with one?) it will add one to your current coin count. You can use the same script to destroy the coin (so it disappears), play a sound (ala mario) and/or emit a particle effect (sparkles, yay!)

There are plenty of other posts explaining how to do this; one titled Collect Items For Points is probably most relevant to you, as it's specifically for coins!

Hope this helps!


To get your code working, all you need to change is:

private var coinToNext:int=10;  //coin necessary to open next level

if ((coinsBegin - coinsCurrent) == coinToNext )
{
   Application.LoadLevel (2);
}

However, I would strongly recommend that you read up on the scripting documentation until you understand what your code is doing. Pasting together everyone else's code without knowing what it does will almost certainly result in a frankengame, and make it very difficult for you to change it to your own needs. I would recommend starting with a tutorial - the 2D Gameplay Tutorial would be a good place to start, given your intended genre!

private var coinsBegin : int;
private var coinsCurrent : int;
var levelToLoad:int;//level to load
private var coinToNext:int=100;//coin necessary to open next level

function OnGUI() 
{
coinsCurrent = 0;
var coinObjects = FindObjectsOfType( GameObject );
for ( var coinObject : GameObject in coinObjects )
{
if ( coinObject.name == "coin" )
coinsCurrent++;
}

GUI.Label( Rect( 0, 0, 320, 20 ), "Collected " + ( coinsBegin - coinsCurrent ) + " of " + coinsBegin + " coins!" );
}

function Start()
{
var coinObjects = FindObjectsOfType( GameObject );
for ( var coinObject : GameObject in coinObjects )
{
if ( coinObject.name == "coin" )
coinsBegin++;
}
}

function Update()
{

if (coinsCurrent == coinToNext )
{
Application.LoadLevel (2);
}
}

sorry wasnt formatted before...

private var coinsBegin   : int;
private var coinsCurrent : int;

function OnGUI() 
{
    coinsCurrent = 0;
    var coinObjects = FindObjectsOfType( GameObject );
    for ( var coinObject : GameObject in coinObjects )
    {
        if ( coinObject.name == "coin" )
            coinsCurrent++;
    }

    GUI.Label( Rect( 0, 0, 320, 20 ), "Collected " + ( coinsBegin - coinsCurrent ) + " of " + coinsBegin + " coins!" );
}

function Start()
{
    var coinObjects = FindObjectsOfType( GameObject );
    for ( var coinObject : GameObject in coinObjects )
    {
        if ( coinObject.name == "coin" )
            coinsBegin++;
    }
}

to clarify, this is code i started with and am trying to modify to load next level after collecting 10 coins...this code counts coins as you collide with them and puts "Collected 0 of 8 Coins!" on screen. works fine for just counting coins...