Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Question Level System

Discussion in 'Scripting' started by Knello, Aug 30, 2020.

  1. Knello

    Knello

    Joined:
    Jul 27, 2020
    Posts:
    50
    Hello, I need help for my game. I guess all of you know games, if the player completes a level and he goes back to menu and press play he plays the next level and not the level which he has completed. I wanna make these system but I don't know how. So I hope here is someone who can help me.

    Here is my Player script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.ComponentModel;
    4. using UnityEngine;
    5. using TMPro;
    6. using UnityStandardAssets.CrossPlatformInput;
    7.  
    8. public class Player : MonoBehaviour
    9. {
    10.     [SerializeField] SpriteRenderer playerImage;
    11.     [SerializeField] TMP_Text playerNameText;
    12.     [SerializeField] ParticleSystem konfettiFx;
    13.  
    14.     public float speed = 1f;
    15.  
    16.     Rigidbody2D rb;
    17.     bool isMoving = false;
    18.     float x, y;
    19.     public GameObject completeLevelUI;
    20.  
    21.  
    22.  
    23.  
    24.     void Start()
    25.     {
    26.         rb = GetComponent<Rigidbody2D> ();
    27.  
    28.         ChangePlayerSkin();
    29.     }
    30.  
    31.     void ChangePlayerSkin()
    32.     {
    33.         Character character = GameDataManager.GetSelectedCharacter();
    34.         if (character.image != null)
    35.         {
    36.             playerImage.sprite = character.image;
    37.             playerNameText.text = character.name;
    38.  
    39.         }
    40.     }
    41.  
    42.    
    43.     void Update()
    44.     {
    45.         x = CrossPlatformInputManager.GetAxis("Horizontal");
    46.         y = CrossPlatformInputManager.GetAxis("Vertical");
    47.  
    48.         isMoving = (x != 0f || y != 0f);
    49.     }
    50.  
    51.     void FixedUpdate()
    52.     {
    53.         if (isMoving)
    54.         {
    55.             rb.position += new Vector2(x, y) * speed * Time.fixedDeltaTime;
    56.            
    57.  
    58.  
    59.         }
    60.     }
    61.  
    62.     void OnCollisionEnter2D (Collision2D other)
    63.     {
    64.         string tag = other.collider.tag;
    65.  
    66.         if (tag.Equals ("Coins"))
    67.         {
    68.             GameDataManager.AddCoins(25);
    69.  
    70.             GameSharedUI.Instance.UpdateCoinsUIText();
    71.  
    72.             Destroy(other.gameObject);
    73.         }
    74.  
    75.         if (tag.Equals("finish"))
    76.         {
    77.             completeLevelUI.SetActive(true);
    78.             konfettiFx.Play();
    79.         }
    80.     }
    81.  
    82.    
    83.  
    84.  
    85. }
    86.  
    and here is my SceneController script

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public static class SceneController
    7. {
    8.     static int mainScene = 0;
    9.     static int sceneToContinue;
    10.    
    11.  
    12.     public static void LoadMainScene ()
    13.     {
    14.         SceneManager.LoadScene(0);
    15.     }
    16.  
    17.     public static void LoadNextScene ()
    18.     {
    19.         int currentScene = SceneManager.GetActiveScene().buildIndex;
    20.         if (currentScene < SceneManager.sceneCountInBuildSettings)
    21.             SceneManager.LoadScene(currentScene + 1);
    22.     }
    23.  
    24.     public static void LoadPreviousScene()
    25.     {
    26.         int currentScene = SceneManager.GetActiveScene().buildIndex;
    27.         if (currentScene > 0)
    28.             SceneManager.LoadScene(currentScene - 1);
    29.     }
    30.  
    31.     public static void LoadScene(int index)
    32.     {
    33.  
    34.         if (index >= 0 && index < SceneManager.sceneCountInBuildSettings)
    35.             SceneManager.LoadScene(index);
    36.     }
    37.  
    38.    
    39. }
    40.  
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,606
    On level completion, save the index of that scene as the lastCompletedLevel.
    Then when pressing Play simply LoadScene(lastCompletedLevel++)
     
    Knello likes this.
  3. Knello

    Knello

    Joined:
    Jul 27, 2020
    Posts:
    50
    Thanks and how i do it?
     
  4. Knello

    Knello

    Joined:
    Jul 27, 2020
    Posts:
    50
    like this?

    Code (CSharp):
    1. public static void lastCompletedLevel()
    2.     {
    3.         int lastCompletedLevel = SceneManager.GetActiveScene().buildIndex;
    4.         if (lastCompletedLevel < 0)
    5.             SceneManager.LoadScene(lastCompletedLevel++);
    6.     }
     
  5. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,606
    I dont know why you check for it to be smaller than zero. Just leave that check out. That's it basically, yes. Just implement a way to set this variable when completing a level (for example by saving it to PlayerPrefs, or making it public static and thus globally accessible), and then get the value when you call that function.
     
    Knello likes this.
  6. Knello

    Knello

    Joined:
    Jul 27, 2020
    Posts:
    50
    And and how should I do that "for example by saving it to PlayerPrefs, or making it public static and thus globally accessible, and then get the value when you call that function."
     
  7. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,606
    Your SceneController is already static, and since the variable has to do with level loading anyways, it would make sense to just add it there. Then set it to the current scene buildIndex whenever you completed a level. If you want this information to persist between sessions (not just scenes) you will have to use PlayerPrefs or any other way or writing information to the drive.
    Code (CSharp):
    1. public static int lastCompletedLevel;
     
    Knello likes this.
  8. Knello

    Knello

    Joined:
    Jul 27, 2020
    Posts:
    50
    Hello again but how do I save it to the player prefs i'm a bloody beginner sorry for annoying you
     
  9. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,606
    You are not annoying me. And everybody was a beginner at one point. Asking questions is what a forum is for. You will find a lot of information with a simple google search tho, especially on super commonly used things such as Unity PlayerPrefs. Just searching for these two words directly turns up the documentation as the first result:
    https://docs.unity3d.com/ScriptReference/PlayerPrefs.html

    There we see that PlayerPrefs comes with a hand full of methods. Mostly one for getting and setting of a couple different data types. Which type are we interrested in? Since you intend to save a scene index, that would be the integer methods, so SetInt and GetInt. Clicking on those in the documentation turns up further explanations and even some examples:
    https://docs.unity3d.com/ScriptReference/PlayerPrefs.SetInt.html
    https://docs.unity3d.com/ScriptReference/PlayerPrefs.GetInt.html

    As you can see there, you can set ("save") an int value to PlayerPrefs by simply writing PlayerPrefs.SetInt("someName", someIntValue);. You can then later load this value using PlayerPrefs.GetInt("someName").

    So when would you do this? You'd save the value once you complete a level. And you'd get the value once you need it to calculate the index of the following scene (which would be that value plus one). So basically when you press the Play button in your main menu, according to your own explanation for what this will be used for.

    Hope this helps, if you have any more questions let me know. Also i hope this gives you an idea for how to look for information yourself, which is a highly important skill you need to learn if you want to programm :)
     
    Knello likes this.
  10. Knello

    Knello

    Joined:
    Jul 27, 2020
    Posts:
    50
    I really appreciate your tips, but I'm still not sure how to set it up in my scripts and does this script make sense or do I have to add something?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public static class SceneController
    7. {
    8.     static int mainScene = 0;
    9.     static int sceneToContinue;
    10.     static int lastCompletedLevel;
    11.  
    12.  
    13.  
    14.  
    15.     public static void start ()
    16.     {
    17.         PlayerPrefs.SetInt("lastcompleteLevel", lastCompletedLevel);
    18.     }
    19.  
    20.     public static void LoadMainScene ()
    21.     {
    22.         SceneManager.LoadScene(0);
    23.     }
    24.  
    25.     public static void LoadNextScene ()
    26.     {
    27.         int currentScene = SceneManager.GetActiveScene().buildIndex;
    28.         if (currentScene < SceneManager.sceneCountInBuildSettings)
    29.             SceneManager.LoadScene(currentScene + 1);
    30.     }
    31.  
    32.     public static void LoadPreviousScene()
    33.     {
    34.         int currentScene = SceneManager.GetActiveScene().buildIndex;
    35.         if (currentScene > 0)
    36.             SceneManager.LoadScene(currentScene - 1);
    37.     }
    38.  
    39.     public static void LoadScene(int index)
    40.     {
    41.  
    42.         if (index >= 0 && index < SceneManager.sceneCountInBuildSettings)
    43.             SceneManager.LoadScene(index);
    44.     }
    45.  
    46.  
    47. }
    48.  
     
  11. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,606
    I assume 'start()' is supposed to be called when you press Play? As i wrote in my last comment, that is where you would Get a previously saved value. You Set the value on level completion, in your other script. In order to load the next level scene you would also need to add a SceneManager.LoadScene(lastCompletedLevel+1) into the method. Also 'start' may be a bad name since it could be confused with Unitys' own Start method.

    I'm leaning a bit out of the window here, but am i right in the assumption that these are not self-written scripts? If you found them online and just want to adjust them that's fine too, but i would strongly recommend you to take a look at good beginner tutorials and get a general understanding of programming in Unity + C# before being stuck at every little change. You wont ever find exactly the script you need online, and will always be required to do little changes to adjust them, at the very least.
     
    Knello likes this.
  12. Knello

    Knello

    Joined:
    Jul 27, 2020
    Posts:
    50
    The "LoadNextScene()" is called when I press the play button. Now I made it like this:

    This is my Player Script
    Code (CSharp):
    1. void OnCollisionEnter2D (Collision2D other)
    2.     {
    3.         string tag = other.collider.tag;
    4.  
    5.         if (tag.Equals ("Coins"))
    6.         {
    7.             GameDataManager.AddCoins(25);
    8.  
    9.             GameSharedUI.Instance.UpdateCoinsUIText();
    10.  
    11.             Destroy(other.gameObject);
    12.         }
    13.  
    14.         if (tag.Equals("finish"))
    15.         {
    16.             completeLevelUI.SetActive(true);
    17.             konfettiFx.Play();
    18.             PlayerPrefs.SetInt("lastcompleteLevel", lastCompletedLevel);
    19.         }
    20.     }
    and this is my scenecontroller script where I load the next scene

    Code (CSharp):
    1. public static void LoadNextScene ()
    2.     {
    3.         int currentScene = SceneManager.GetActiveScene().buildIndex;
    4.         if (currentScene < SceneManager.sceneCountInBuildSettings)
    5.             SceneManager.LoadScene(currentScene + 1);
    6.         PlayerPrefs.GetInt("lastcompleteLevel", lastCompletedLevel);
    7.         SceneManager.LoadScene(lastCompletedLevel + 1);
    8.     }
    But I guess it would be better if I do 2 seperate functions for "start" and "nextlevel" or?
     
  13. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,606
    Yes you should create two methods, as LoadNextScene implies by name to load currentScene+1. What you want would be harder to indicate by a small method name, but just calling it Play should be fine aswell, if that's what's supposed to happen when you press Play.
    Also, currently you have two LoadScene in one method. I'm not exactly sure what the engine even does in this case, but i imagine it will try to load one scene, then the other, which is simply a waste of ressources.
     
    Knello likes this.
  14. Knello

    Knello

    Joined:
    Jul 27, 2020
    Posts:
    50
    Now I did it like this:

    Code (CSharp):
    1. public static void StartGame()
    2.     {
    3.  
    4.         PlayerPrefs.GetInt("lastcompleteLevel", lastCompletedLevel);
    5.         SceneManager.LoadScene(lastCompletedLevel + 1);
    6.     }
    But after I played a level and go back to main menu and press play the same level loads again instead of the next one.
     
  15. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,606
    Do you set lastcompletelevel in PlayerPrefs after completing a level? I assume not if the loaded level does not change.
     
    Knello likes this.
  16. Knello

    Knello

    Joined:
    Jul 27, 2020
    Posts:
    50
    I did it like this, do I need a method for it? If yes I really don't know how to do it.

    Player Script:

    Code (CSharp):
    1.  void OnCollisionEnter2D (Collision2D other)
    2.     {
    3.         string tag = other.collider.tag;
    4.  
    5.         if (tag.Equals ("Coins"))
    6.         {
    7.             GameDataManager.AddCoins(25);
    8.  
    9.             GameSharedUI.Instance.UpdateCoinsUIText();
    10.  
    11.             Destroy(other.gameObject);
    12.         }
    13.  
    14.         if (tag.Equals("finish"))
    15.         {
    16.             completeLevelUI.SetActive(true);
    17.             konfettiFx.Play();
    18.             PlayerPrefs.SetInt("lastcompleteLevel", lastCompletedLevel);
    19.         }
    20.     }
     
  17. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,606
    There are two problems with what you wrote. Your first mistake is here actually. You need to assign the loaded value to something. Which is also shown in the documentation, and the examples i linked you. So change your GetInt part to this:
    Code (CSharp):
    1. lastCompletedLevel = PlayerPrefs.GetInt("lastcompleteLevel");
    For the SetInt part you need to assign the value of the current scene. So get the scene build index like you are in your main script. That is the value you are trying to save there. I dont want to sound rude, but that's what you intend to do, right? In the scene the player last completed, save a value, such that we can load this value, in order to start at the next level. The value in question is the build index of the level just completed, ie the current scene.

    And you really do yourself a favor going through some beginner tutorials. These are very basic problems you are currently stumbling over. Not to offend you, but it feels like you are guessing what to do, just pasting random pieces of code somewhere and hoping it works. In the end it's of course up to you, but you will save time by improving your fundamentals.
     
    Last edited: Sep 2, 2020
    Knello likes this.
  18. Knello

    Knello

    Joined:
    Jul 27, 2020
    Posts:
    50
    Can you show me how I should do it I really don't know. I try to learn by doing or just say I try to understand what was done and to comprehend it. I often watch tutorials but I than I can't apply it to my scripts.
     
  19. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,606
    I already told you how the GetInt part should look like:
    Code (CSharp):
    1. lastCompletedLevel = PlayerPrefs.GetInt("lastcompleteLevel");
    2. SceneManager.LoadScene(lastCompletedLevel+1)
    Write that when you want to load the next level, ie when you press Play.

    For the SetInt part, as i already mentioned above as well, you need to actually save the scene you just completed. So on completing a level, get the index of the scene and then save that value to your PlayerPrefs variable.
    Code (CSharp):
    1. int currentScene = SceneManager.GetActiveScene().buildIndex;
    2. PlayerPrefs.SetInt("lastcompleteLevel", currentScene);
    So now once you complete a level, for example level 21, you save this value '21' under the name of "lastcompleteLevel". When you are in your main menu and press play, we load the value from "lastcompleteLevel", add one, and then load the scene with that number, ie 22.

    Programming is learning by doing. Dont just watch a tutorial. Do the tutorial. You will want to follow along, implement what the person in the tutorial does, and potentially experiment around with it a bit. Also, you will want to try and understand what the script does, which part of it does what and how it achieves what it does. Then in the future, when you write a script, you have some more tools in your toolbelt, because you already did something like that when you followed a tutorial. Or at least you have a slight idea how to approach the problem because you did something similar, or know which parts of something you did before may be useful for what you do now. It's just about adding tools to your toolbelt. But without actually programming something, you wont know how / for what to use these tools, and thus they will remain useless. That's why it's learning by doing.
    A great place to start (and follow along with the little exercises), would be here:
     
    Knello likes this.
  20. Knello

    Knello

    Joined:
    Jul 27, 2020
    Posts:
    50
    Thank you very very much man, now I will learn something about c# and unity. I guess without you I would just stop programming and I don't know... Thanks that you took your time for me. I really appreciate it.
     
    Yoreki likes this.