Search Unity

How do i do this?

Discussion in 'Scripting' started by Andrew4Games, Jul 20, 2014.

  1. Andrew4Games

    Andrew4Games

    Joined:
    Jul 17, 2014
    Posts:
    16
    Well i did fix that i could load level 2 for my game in the script but how do i let it load level 3 after level 2 in this script?
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GameManager : MonoBehaviour {
    5.    
    6.     public GameObject player;
    7.     private GameObject currentPlayer;
    8.     private GameCamera cam;
    9.     private Vector3 checkpoint;
    10.  
    11.     public static int levelCount = 2;
    12.     public static int currentLevel = 1;
    13.     public static int secondLevel = 2;
    14.  
    15.  
    16.     void Start () {
    17.         cam = GetComponent<GameCamera>();
    18.  
    19.         if (GameObject.FindGameObjectWithTag("Spawn")) {
    20.             checkpoint = GameObject.FindGameObjectWithTag("Spawn").transform.position;
    21.         }
    22.  
    23.         SpawnPlayer(checkpoint);
    24.     }
    25.    
    26.     // Spawn player
    27.     private void SpawnPlayer(Vector3 spawnPos) {
    28.         currentPlayer = Instantiate(player,spawnPos,Quaternion.identity) as GameObject;
    29.         cam.SetTarget(currentPlayer.transform);
    30.     }
    31.  
    32.     private void Update() {
    33.         if (!currentPlayer) {
    34.             if (Input.GetButtonDown("Respawn")) {
    35.                 SpawnPlayer(checkpoint);
    36.             }
    37.         }
    38.     }
    39.  
    40.     public void SetCheckpoint(Vector3 cp) {
    41.         checkpoint = cp;
    42.     }
    43.  
    44.     public void EndLevel() {
    45.         if (currentLevel < levelCount) {
    46.             currentLevel++;
    47.             Application.LoadLevel("Level" + secondLevel);
    48.        
    49.  
    50.        
    51.     }
    52.         else {
    53.             Debug.Log("Game Over");
    54.         }
    55.  
    56.  
    57.    
    58.             }
    59.         }
    Because the level 2 is int secondlevel = 2;
    and i added it under the endlevel? but how do i load the level 3 scene on it?
     
  2. Grim_Reaper

    Grim_Reaper

    Joined:
    Nov 5, 2013
    Posts:
    32
    If you want to load a scene in addition to the scene already running, you need use loadleveladditive. If you want to use loadlevel, pass the Name of the level to this method.
     
  3. Andrew4Games

    Andrew4Games

    Joined:
    Jul 17, 2014
    Posts:
    16
    Could you type the exact code i need to do for loading level 3
     
  4. Glockenbeat

    Glockenbeat

    Joined:
    Apr 24, 2012
    Posts:
    670
    Get the currently loaded level index with Application.loadedLevel, increase it by 1, and use Application.LoadLevel with the new value.
     
  5. Andrew4Games

    Andrew4Games

    Joined:
    Jul 17, 2014
    Posts:
    16
    Well what's the exact code? is it Application.loadedlevel(1) or application.loadedlevel("level" + secondlevel); or what????
    I don't know what you mean.
     
  6. Glockenbeat

    Glockenbeat

    Joined:
    Apr 24, 2012
    Posts:
    670
  7. Andrew4Games

    Andrew4Games

    Joined:
    Jul 17, 2014
    Posts:
    16
    Buuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuut hooooooooooooooooooooowwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
     
  8. dterbeest

    dterbeest

    Joined:
    Mar 23, 2012
    Posts:
    389
    this kind of behaviour is not going to get you anywhere... really

     
  9. novashot

    novashot

    Joined:
    Dec 12, 2009
    Posts:
    373
    get rid of secondLevel var... you already have the functionality built in and are blinded apparently by forum watching...

    Code (csharp):
    1.  
    2. public void EndLevel() {
    3.      if (currentLevel < levelCount) {
    4.             currentLevel++;
    5.           ///Application.LoadLevel("Level" + secondLevel);
    6.          Application.LoadLevel("Level" + currentLevel); // use the var you already set for the new level otherwise it's wasted and not needed
    7.       }              
    8.    }
    9.  
     
  10. keenanwoodall

    keenanwoodall

    Joined:
    May 30, 2014
    Posts:
    598
    Dude, to get help you gotta be mature! And at least post your problem in the thread title
     
  11. Andrew4Games

    Andrew4Games

    Joined:
    Jul 17, 2014
    Posts:
    16
    Well didn't fix anything because it won't load the level 3 scene after it says next stage.
     
  12. Glockenbeat

    Glockenbeat

    Joined:
    Apr 24, 2012
    Posts:
    670
    *sigh*

    Code (CSharp):
    1.  
    2. public void EndLevel()
    3. {
    4.    int nextLevel = Application.loadedLevel + 1;
    5.    
    6.    if (nextLevel < Application.levelCount)
    7.    {
    8.      Application.LoadLevel(nextLevel);
    9.    }
    10.    else
    11.    {
    12.      int i = 0;
    13.      while (i < 1000)
    14.      {
    15.        Debug.Log("I shall be friendly to people and empower myself to learn how to code!");
    16.        i++;
    17.      }
    18.    }
    19. }
    20.  
     
    keenanwoodall likes this.
  13. Andrew4Games

    Andrew4Games

    Joined:
    Jul 17, 2014
    Posts:
    16
    Lol, i haven't even typed bad S*** to anyone -.-
     
  14. novashot

    novashot

    Joined:
    Dec 12, 2009
    Posts:
    373
    if it didn't load it then you have it named differently... maybe use the scene number instead of the name if you have so it doesn't require a certain naming convention to load.
     
  15. Andrew4Games

    Andrew4Games

    Joined:
    Jul 17, 2014
    Posts:
    16
    Nova its fixed, but thanks for telling me :)