Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Returning Level Map

Discussion in 'Scripting' started by Paykoman, Oct 21, 2014.

  1. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    Hi guys im trying to back level map after all my enemies are dead in current level but i cant figure it out. So my enemie script right now are this one down and it spawn 50 enemies of each type. How can i send game to levels map when all die?

    Thx
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class EnemyManager2 : MonoBehaviour
    4. {
    5.     public PlayerHealth playerHealth;
    6.     public GameObject enemy;
    7.     public float spawnTime = 3f;
    8.     public Transform[] spawnPoints;
    9.    
    10.    
    11.     void Start ()
    12.     {
    13.         InvokeRepeating ("Spawn", spawnTime, spawnTime);
    14.     }
    15.    
    16.     int counter = 0;
    17.     int currentLevelMobAmount = 20;
    18.    
    19.     void Spawn()
    20.     {
    21.         if (counter >= currentLevelMobAmount)
    22.         {
    23.             CancelInvoke();
    24.            
    25.             // reset counter
    26.             counter = 0;
    27.            
    28.             // can set amount of enemies for new level
    29.             // then reinvoke this method when needed
    30.             currentLevelMobAmount += 10;
    31.         }
    32.        
    33.         if (playerHealth.currentHealth <= 0f)
    34.         {
    35.             return;
    36.         }
    37.        
    38.         int spawnPointIndex = Random.Range(0, spawnPoints.Length);
    39.        
    40.         Instantiate(enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
    41.        
    42.         counter++;
    43.     }
    44. }
    45.  
    46.  
     
  2. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    Any help or suggestion will be welcome since i already try everything i can :)
     
  3. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
  4. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    My level is in differente scene wt i want is after all enemies die game will show score and go directly to game map that there is in another scene like when u over a candy crush level game return to levels map...
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,985
    Look up the Application.LoadLevel() function call.

    You need to add the scenes you want to be reachable to the Editor Build Settings.

    The basic logic would be: when all enemies are gone (enemy count reaches zero), wait a few seconds and call Application.LoadLevel() with the name of your main map level.

    The same thing will happen in the map level: when you click on a given part of the map, each part would go to its own particular level to play that area.

    Kurt
     
  6. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    I try change the code but wt i find is with the alteration when level start after 2s he goes to menu... probably im not doing the counter right...
    Code (CSharp):
    1. public class EnemyManager2 : MonoBehaviour
    2. {
    3.     public PlayerHealth playerHealth;
    4.     public GameObject enemy;
    5.     public float spawnTime = 3f;
    6.     public Transform[] spawnPoints;
    7.    
    8.    
    9.     void Start ()
    10.     {
    11.         InvokeRepeating ("Spawn", spawnTime, spawnTime);
    12.     }
    13.    
    14.     int counter = 0;
    15.     int currentLevelMobAmount = 10;
    16.    
    17.     void Spawn()
    18.     {
    19.         if (counter >= currentLevelMobAmount)
    20.         {
    21.             CancelInvoke();
    22.            
    23.             // reset counter
    24.             counter = 0;
    25.            
    26.             // can set amount of enemies for new level
    27.             // then reinvoke this method when needed
    28.             currentLevelMobAmount += 10;
    29.         }
    30.        
    31.         if (playerHealth.currentHealth <= 0f)
    32.         {
    33.             return;
    34.         }
    35.        
    36.         int spawnPointIndex = Random.Range(0, spawnPoints.Length);
    37.        
    38.         Instantiate(enemy, spawnPoints[spawnPointIndex].position, spawnPoints[spawnPointIndex].rotation);
    39.        
    40.         counter++;
    41.  
    42.         if (counter<currentLevelMobAmount)
    43.         {
    44.             Application.LoadLevel("World1");
    45.         }
    46.     }
    47. }
    48.