Search Unity

Ending a Game

Discussion in 'Scripting' started by Velketor, Oct 23, 2007.

  1. Velketor

    Velketor

    Joined:
    Sep 15, 2007
    Posts:
    110
    Hello,

    I am still pretty new to scripting and I was wondering if anyone could explain to me how to END a game...

    At the moment, I am under the impression that whenever the player gets X amount of points (or whatever your goal is) a "Winner" screen will pop up...

    How do I go about creating such a scenario? Any information is greatly appreciated!

    Thanks!

    -Velk
     
  2. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    What I did for an game ending is as follows. Basically two types of enemy were obliterated, and a few other things went down:
    Code (csharp):
    1. var SubShadow: GameObject;
    2. var Sub: GameObject;
    3. var Manager: GameObject;
    4. var EndGui: GameObject;
    5. var newObject : Transform;
    6.  
    7. function Update(){
    8.   var enemies1 = GameObject.FindGameObjectsWithTag("SubBase");
    9.   var enemies = GameObject.FindGameObjectsWithTag("Enemy");
    10.   if (enemies.Length <= 0  enemies1.Length <= 0)
    11.   {     EndGui.active=true;
    12.            Debug.Log("No more baddies");
    13.            
    14.     Instantiate(newObject, transform.position, transform.rotation);
    15. Destroy(Manager);
    16.         Destroy(Sub);
    17.             Destroy(SubShadow);
    18.          Destroy(gameObject);
    19.   }
    20. }
    The Sub was my "Player" ie what the player was controlling/shooting from in the scene. The Manager was something with scripts on it, I destroy it so as not to get null references leading to errors and therefore problems. End gui is a GUI texture with congratulations or whatever written on it, and newObject was just a game object with a script to load the initial start screen again.

    I use Tags (see top of inspector) to define my enemies, and when they are all destroyed, ie none there in the scene, the gui appears saying you won, the script to load the splash after 5 seconds is instantiated, and the player, and the shadow are destroyed, (again to avoid null references) and you wind up back at the splash screen, where you either click quit, or play again.

    If you search for "score" in the forum, you should find some code to help out in a score based sense, or maybe someone will just post it for you.

    Hope this helps!
    AC
     
  3. Velketor

    Velketor

    Joined:
    Sep 15, 2007
    Posts:
    110
    Extremely helpful...thank you so much!

    --Velk