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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

How to start next level ?

Discussion in 'Scripting' started by NjordNystrom, May 5, 2020.

  1. NjordNystrom

    NjordNystrom

    Joined:
    Feb 29, 2016
    Posts:
    25
    Hello, im try to achieve making level manager but i have no idea how to make it. I have 30 level. When i reach the last part of Level 1, im gonna show success click to next level UI and start beginning checkpoint of Level 2.

    I have waypoints. This scripts adds waypoints and my chracters follow them. I put waypoints manager other level and set empty gameobjects as waypoints but i have no idea how to send my character other level starter points when finish level and click button. I need to save level data because if player exit from game at level 10, he must be start at 10 lvl when he open the game. Basically, what i need, how can i check my current level, i need save and load but i never do this system before and im stuck here !
     
  2. pantang

    pantang

    Joined:
    Sep 1, 2016
    Posts:
    219
    You will have to adapt this to suit but heres a snippit to save data(ints in this case) to a text file and load it back up...

    Code (CSharp):
    1.     private void SaveCash(string path, int cash, int bit)
    2.     {
    3.         //creat our array and convert our stock ints into strings
    4.         string[] saveData = { cash.ToString(), bit.ToString() };
    5.  
    6.         //delete the old data
    7.         if (File.Exists(path))
    8.             File.Delete(path);
    9.         if (!File.Exists(path))
    10.         {
    11.             // Create a file to write to.
    12.             File.WriteAllLines(path, saveData, Encoding.UTF8);
    13.         }
    14.     }
    15.     private void LoadCash(string path) //load our player data if we have any
    16.     {
    17.         if (File.Exists(path))
    18.         {
    19.             string[] loadData = new string[0];
    20.             loadData = File.ReadAllLines(path);
    21.             GetComponent<Inventory>().cash = int.Parse(loadData[0]);
    22.             GetComponent<Inventory>().bit = int.Parse(loadData[1]);
    23.         }
    24.     }
    As for loading you just want to change "level" to the next scene in your game

    SceneManager.LoadScene(level);

    Heres a script I use to manage my scenes and put them into a scollbox

    you will need to make a ui prefab with a textmesh pro text and a button that goes in the scroll content for this to work and you will need to remove the references to other scripts but should be easy enough to modify to your needs. Then just place it on a tigger and you should be good to go.

    Code (CSharp):
    1. using TMPro;
    2. using UnityEngine;
    3. using UnityEngine.SceneManagement;
    4. using UnityEngine.UI;
    5. namespace UnityStandardAssets.Characters.ThirdPerson
    6. {
    7.     public class BusStop : MonoBehaviour
    8.     {
    9.  
    10.         string[] mapNames;
    11.         GameObject[] maps;
    12.  
    13.         bool interact;
    14.  
    15.         public string BusStopText;
    16.         public float contactSpacing = 100;
    17.  
    18.         private void Start()
    19.         {
    20.             mapNames = FindObjectOfType<WorldData>().mapNames;
    21.             System.Array.Resize(ref maps, mapNames.Length);
    22.         }
    23.  
    24.         private void OnTriggerEnter(Collider other)
    25.         {
    26.             if (other.gameObject.CompareTag("Player") && other.gameObject.GetComponent<PlayerData>().menuActive == false)
    27.             {
    28.                 interact = true;
    29.                 other.gameObject.GetComponent<PlayerUI>().uiOneLineText.text = BusStopText;
    30.                 other.gameObject.GetComponent<PlayerUI>().uiOneLine.SetActive(true);
    31.                 other.gameObject.GetComponent<PlayerData>().menuActive = true;
    32.                 //AudioTools.PlayRandomClip(greetingClip, GetComponent<AudioSource>());
    33.             }
    34.         }
    35.         private void OnTriggerStay(Collider other)
    36.         {
    37.             //check no one is talking or chasing
    38.             if (interact == true)
    39.             {
    40.                 //wait for a key press then open the menu
    41.                 if (Input.GetButtonDown("Interact") && other.gameObject.CompareTag("Player"))
    42.                 {
    43.                     other.gameObject.GetComponent<PlayerData>().menuActive = true;
    44.                     OpenMenu(other);
    45.                     other.gameObject.GetComponent<PlayerUI>().busStopUI.SetActive(true);
    46.                 }
    47.             }
    48.         }
    49.  
    50.         private void OnTriggerExit(Collider other)  //sets home on aicontrol when player no longer in colider
    51.         {
    52.             //make sure we were active with the player if so close the menu
    53.             if (other.gameObject.CompareTag("Player") && interact == true)
    54.             {
    55.                 interact = false;
    56.                 other.gameObject.GetComponent<PlayerData>().menuActive = false;
    57.                 other.gameObject.GetComponent<PlayerUI>().uiOneLine.SetActive(false);
    58.                 //AudioTools.PlayRandomClip(goodbyeClip, GetComponent<AudioSource>());
    59.             }
    60.         }
    61.  
    62.         private void OpenMenu(Collider other)
    63.         {
    64.             float posY = other.gameObject.GetComponent<PlayerUI>().busDestinationPrefab.GetComponent<RectTransform>().localPosition.y;
    65.             float posX = other.gameObject.GetComponent<PlayerUI>().busDestinationPrefab.GetComponent<RectTransform>().localPosition.x;
    66.             float posZ = other.gameObject.GetComponent<PlayerUI>().busDestinationPrefab.GetComponent<RectTransform>().localPosition.z;
    67.             posY = -100;
    68.  
    69.             //set close script to first button
    70.             Button[] close = other.gameObject.GetComponent<PlayerUI>().busStopUI.GetComponentsInChildren<Button>();
    71.             close[0].onClick.AddListener(() => CloseMenu(other));
    72.  
    73.             //find out what scene we are in
    74.             Scene currentScene = SceneManager.GetActiveScene();
    75.             int buildIndex = currentScene.buildIndex;
    76.  
    77.             for (int i = 0; i < mapNames.Length; i++)
    78.             {
    79.                 if (i == buildIndex) { } //dont display our current scene
    80.                 else
    81.                 {
    82.                     //create new contact, set the canvas active and add to array for detruction later
    83.                     GameObject destination = Instantiate(other.gameObject.GetComponent<PlayerUI>().busDestinationPrefab);
    84.                     destination.SetActive(true);
    85.                     destination.transform.SetParent(other.gameObject.GetComponent<PlayerUI>().busDestinationPrefab.transform.parent);
    86.                     maps[i] = destination;
    87.  
    88.                     //set position of our map
    89.                     destination.GetComponent<RectTransform>().localPosition = other.gameObject.GetComponent<PlayerUI>().busDestinationPrefab.GetComponent<RectTransform>().localPosition;
    90.                     destination.GetComponent<RectTransform>().localPosition = new Vector3(posX, posY, posZ);
    91.                     destination.GetComponent<RectTransform>().localScale = other.gameObject.GetComponent<PlayerUI>().busDestinationPrefab.GetComponent<RectTransform>().localScale;
    92.                     posY = posY - contactSpacing;
    93.  
    94.                     //set the destination name
    95.                     destination.GetComponentInChildren<TextMeshProUGUI>().text = mapNames[i];
    96.  
    97.                     //Set scene to load
    98.                     int num = i;
    99.                     Button[] button = destination.GetComponentsInChildren<Button>();
    100.                     button[0].onClick.AddListener(() => LoadScene(num, other));
    101.                 }
    102.             }
    103.             other.gameObject.GetComponent<PlayerUI>().busScroll.sizeDelta = new Vector2(5, Mathf.Abs(posY));
    104.         }
    105.  
    106.         private void CloseMenu(Collider other)  //destroy the listeners we created for the buttons and close the UI
    107.         {
    108.             interact = false;
    109.             other.gameObject.GetComponent<PlayerData>().menuActive = false;
    110.             other.gameObject.GetComponent<PlayerUI>().uiOneLine.SetActive(false);
    111.             other.gameObject.GetComponent<PlayerUI>().busStopUI.SetActive(false);
    112.             for (int i = 0; i < maps.Length; i++)
    113.             {
    114.                 Destroy(maps[i]);
    115.             }
    116.         }
    117.  
    118.         private void LoadScene(int level, Collider other)
    119.         {
    120.             //save the players inventory
    121.             other.gameObject.GetComponent<PlayerData>().SaveAllData();
    122.  
    123.             //find all storage containers in scene
    124.             ItemStorage[] components = Resources.FindObjectsOfTypeAll<ItemStorage>();
    125.  
    126.             //save the storage containers(must have uniques names set in item storage)
    127.             for (int i = 0; i < components.Length; i++)
    128.             {
    129.                 components[i].GetComponent<ItemStorage>().SaveStorage();
    130.             }
    131.             SceneManager.LoadScene(level);
    132.         }
    133.     }
    134. }
    And I use this code to get the map names in another script.

    Code (CSharp):
    1.         Array.Resize(ref mapNames, SceneManager.sceneCountInBuildSettings);
    2.         for (int i = 0; i < SceneManager.sceneCountInBuildSettings; i++)
    3.         {
    4.             mapNames[i] = Path.GetFileNameWithoutExtension(SceneUtility.GetScenePathByBuildIndex(i));
    5.         }
     
  3. NjordNystrom

    NjordNystrom

    Joined:
    Feb 29, 2016
    Posts:
    25
    Thank you for your time and your answer. Let me check these.