Search Unity

need help with load level script...$$

Discussion in 'Scripting' started by etomp10291, Sep 26, 2010.

  1. etomp10291

    etomp10291

    Joined:
    Jan 11, 2010
    Posts:
    108
    Hey guys!

    I am using this basic "coin" counting script to count items picked up in a scene:

    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++;
    }
    }


    what i can't figure out is how to modify so that after they pickup "x" amount of items it will trigger next level to load which would be a win screen or next game level.

    i am stuck on this, i can paypal someone a couple bucks if you can send me a quick example project so i can figure it out...thanks!

    PM me if you can help.
     
  2. devilkkw

    devilkkw

    Joined:
    Jun 24, 2009
    Posts:
    175
    Code (csharp):
    1. private var coinsBegin : int;
    2. private var coinsCurrent : int;
    3. var levelToLoad:int;//level to load
    4. private var coinToNext:int=100;//coin necessary to open next level
    5.  
    6. function OnGUI()
    7. {
    8. coinsCurrent = 0;
    9. var coinObjects = FindObjectsOfType( GameObject );
    10. for ( var coinObject : GameObject in coinObjects )
    11. {
    12. if ( coinObject.name == "coin" )
    13. coinsCurrent++;
    14. }
    15.  
    16. GUI.Label( Rect( 0, 0, 320, 20 ), "Collected " + ( coinsBegin - coinsCurrent ) + " of " + coinsBegin + " coins!" );
    17. }
    18.  
    19. function Start()
    20. {
    21. var coinObjects = FindObjectsOfType( GameObject );
    22. for ( var coinObject : GameObject in coinObjects )
    23. {
    24. if ( coinObject.name == "coin" )
    25. coinsBegin++;
    26. }
    27. }
    28.  
    29. function Update()
    30. {
    31.  
    32. if (coinsCurrent == coinToNext )
    33. {
    34. Application.LoadLevel (levelToLoad);
    35. }
    36. }
    37.  
    38.  
     
  3. MrPenguin

    MrPenguin

    Joined:
    Dec 30, 2008
    Posts:
    70
    It seems as if you are finding all the GameObjects in your scene. What you might want to do is just find the coins based on their tag.

    But as for running an action when you collect x amount of coins, here is what I think you should do:

    For your GUI/player script:

    Code (csharp):
    1. private var coinsBegin : int;
    2. static var coinsCurrent : int;
    3.  
    4. function OnGUI()  {
    5.     GUI.Label( Rect( 0, 0, 320, 20 ), "Collected " + ( coinsBegin - coinsCurrent ) + " of " + coinsBegin + " coins!" );
    6. }
    7.  
    8. function Start() {
    9.     var coinObjects = FindObjectsOfType( GameObject );
    10.  
    11.     for ( var coinObject : GameObject in coinObjects ) {
    12.         if ( coinObject.name == "coin" ) coinsBegin++;
    13.     }
    14. }
    For your coin script. Replace "yourScript" with the name of your previous script.

    Code (csharp):
    1.  
    2. var coinsToPickUp : int = 5;
    3.  
    4. function OnTriggerEnter (col : Collider) {
    5.     yourScript.coinsCurrent++;
    6.     gameObject.Destroy(gameObject); // Destroy the gameObject to give the illusion of picking up the coin.
    7.  
    8.     if (yourScript.coinsCurrent == coinsToPickUp) {
    9.         // Run your code here
    10.     }
    11. }
    12.  
    Does this work?
     
  4. etomp10291

    etomp10291

    Joined:
    Jan 11, 2010
    Posts:
    108
    unfortunately neither code worked. thanks for trying.

    My original code was used from "iPhone - 3D Game Programming - All in one" book. I am working with the example project.

    when i run the example project, the code above simply counts coins as you collide with them and adds GUIText to screen saying "Collected 0 of 8 Coins!". As i said i just need to modify script so that after i collect the 8 coins the next level will load.

    it appears i need a script for GUI and a script to place on coin object.

    Thanks!
     
    Last edited: Sep 27, 2010
  5. etomp10291

    etomp10291

    Joined:
    Jan 11, 2010
    Posts:
    108
    when i run with this code, next level loads immediately without collecting coins...thanks
     
  6. etomp10291

    etomp10291

    Joined:
    Jan 11, 2010
    Posts:
    108
    when i run with this code, it shows "collected 8 of 8 coins!" before actually collecting any coins, and level does not load...thanks!
     
  7. devilkkw

    devilkkw

    Joined:
    Jun 24, 2009
    Posts:
    175
    Code (csharp):
    1. function Start()
    2. {
    3. var coinObjects = FindObjectsOfType( GameObject );
    4. for ( var coinObject : GameObject in coinObjects )
    5. {
    6. if ( coinObject.name == "coin" )
    7. coinsBegin++;
    8. }
    9. }
    10.  
    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.

    Code (csharp):
    1. function OnCollisionEnter(hit : Collision)
    2. {
    3.     if (hit.transform.tag == "Coins") { // remember assign Coins tag to our coins game object
    4.    
    5. //altrnate try it " if (hit.GameObject.name == "coin")  "  but im' not sure it works.
    6.     coinsBegin++;
    7.     }
    8. }
     
    Last edited: Sep 27, 2010
  8. etomp10291

    etomp10291

    Joined:
    Jan 11, 2010
    Posts:
    108
    thank you!!!