Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Enemy Counter

Discussion in 'Scripting' started by Gab Steve, May 2, 2014.

  1. Gab Steve

    Gab Steve

    Joined:
    Nov 3, 2012
    Posts:
    33
    Hey guys im just wondering if anyone could help me make a script that counts the enemy's left and when there are 0 enemy's left it goes to the next level or a menu pops up and shows them there points and lets then hit next to go to the next level. If anyone could help me with that and also a script that keeps score that you get from killing enemys and stores it so you can see it in the main menu as well. Thanks.(Im not asking you to do all the work just want the logic for going about this) .
     
  2. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Hm, at the moment i can think about two simple ways to do that.

    I personally would take the second one, because it's much more dynamic if you want to add enemies during the level as well.

    1) Name and / or your enemies and at the beginning of the level, find all objects with the name / tag which will return an array, but you'd only need to get it's length. This will be the total numbers of enemies. Whenever you kill one enemy, decrement the number by 1. I wouldn't do it this way though.

    2) This is the way i would spontaneously do it:
    One script that manages the amount of enemies, this can be a new script or be added to an existent scrip and the counter variable in it could also be a static one.
    Now, as your level starts, Awake and soon Start functions will be called which means you can use them in order to increment the static counter by 1.
    I personally think it's the better solution as you can always spawn new enemies which all will have the Awake (or Start) function and thus increment the counter again. This way, you don't really need to find all the objects with a specific name or tag all the time in order to count them.

    Of course, if they die (disbaled/destroyed) they should decrease the counter by 1 too.
    ______________________

    Now as for the loading of the level and showing the button or text:

    In OnGUI, test whether the counter is greater than 0 or not. If it is, you won't show the message/the button, whatever you want to use for it.
    When it is 0 (or less, which shouldn't happen though), you can show the message or button on the screen. Load the level when the necessary button has been pressed.

    The score can simply be hold in a variable during gameplay and as soon as you finish the game, you can save it's content to PlayerPrefabs. In your main menu, get the value of the highscore from the PlayerPrefabs and just show it via OnGUI.
     
    Last edited: May 2, 2014
  3. Gab Steve

    Gab Steve

    Joined:
    Nov 3, 2012
    Posts:
    33
    Hi thanks for your quick reply. Im making a mobile game(android), and im new to mobile programming etc. So i like the logic you have given in the first part its very well explained and i will go with that because i wont be spawning enemy's in any of the levels, but the score thing is a little confusing because i dont know how to use playerprefabs, can you show me ?
     
  4. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    PlayerPrefs

    Example for saving an integer:

    Code (csharp):
    1.  
    2. PlayerPrefs.SetInt("yourKey", 30); // second paramter can be i.e. your score variable, i simply used a constant value here for demonstration
    3. int a = PlayerPrefs.GetInt("yourKey");  // this way you can read out the saved value again
    4.  
    There are also functions for floats and strings, just check the documentation.
     
  5. Gab Steve

    Gab Steve

    Joined:
    Nov 3, 2012
    Posts:
    33
    Thanks man!
     
  6. Gab Steve

    Gab Steve

    Joined:
    Nov 3, 2012
    Posts:
    33
    Hey can you help me with touch stuff? Like when i make the players stats come up after the level how will i make a GUI/3d text that clicks when the user touches it you know what i mean?
     
  7. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    I do not have any experience with mobile development, but you could make a button with the text or stats you would like to be clickable.
    Or a 3D text and use raycasts in order to see if any object was 'hit' by that touch.
    I cannot help you with any further and detailed touch-related stuff though :/
     
  8. Gab Steve

    Gab Steve

    Joined:
    Nov 3, 2012
    Posts:
    33
    OK np thanks
     
  9. Gab Steve

    Gab Steve

    Joined:
    Nov 3, 2012
    Posts:
    33
    When making the enemy Counter im confused with what i should do to make the array reduce by 1.

    Code (csharp):
    1. #pragma strict
    2.  
    3. var enemyPlayers : GameObject[];
    4.  
    5. function Start () {
    6.  
    7. }
    8.  
    9. function Update ()
    10. {
    11.     var enemyHealth : CharacterDamage;
    12.     enemyHealth = GetComponent("CharacterDamage");
    13.    
    14.     if(enemyHealth.hitPoints == 0)
    15.     {
    16.         enemyPlayers.Size = 2; // < over there i dont know how to make the size reduce by 1. PLease help
    17.     }
    18. }
    19.  
    20.  
    21.  
    22. function OnGUI ()
    23. {
    24.     if(enemyPlayers == 0)
    25.     {
    26.         if(GUI.Button(Rect(10,10,50,50),"NextLvl"))
    27.         {
    28.             Application.LoadLevel("loadedLevel");
    29.         }
    30.     }
    31. }
     
  10. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Does 'Size' even exist or is it just meant to be pseudo code? I'm not coding alot in JS but this type of array is most likely not dynamic and cannot be resized. Someone please correct me if i'm wrong.

    So, where do you get the content for the array from? Is there missing anything?
    In order to count your enemies this way, you should get a temporarily array of your enemies and set an int variable to the length of the array. That's your counter that you can work with instead. This way you don't need to count the elements of the array which still hold an enemy.

    If you still want to use an array type, you should rather think about an dynamic array which can be resized. Then you could always remove an enemy while it's length will be adjusted and you could take the length as your counter.

    I'd go for the int version though.
     
  11. Gab Steve

    Gab Steve

    Joined:
    Nov 3, 2012
    Posts:
    33
    Lol you're right this kind of array isnt dynamic, and size was simply one of the components i was trying to use to see if it will work to reduce itself :p.

    Anyway i have already made another script that counts the enemy kills and according to that it will put up the next level button and GUI menu's thanks anyway though!
     
  12. Gab Steve

    Gab Steve

    Joined:
    Nov 3, 2012
    Posts:
    33
    how do i make a dynamic array?
     
  13. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
  14. Gab Steve

    Gab Steve

    Joined:
    Nov 3, 2012
    Posts:
    33
    Ahh ok thanks. Also im really new to using playerprefs and i dont know how to save my player kills value. And when i go to the stats menu i want it to show my kills. Basically i have a Kills GUI script in game that counts my kills and i want that to show in the stats as well. How would i do this i just dont know where to start? The playerprefs in the unity docs isnt very helpful.........
     
    Last edited: May 9, 2014
  15. Gab Steve

    Gab Steve

    Joined:
    Nov 3, 2012
    Posts:
    33

    Np i've got it thanks.