Search Unity

Set Game Difficulty By Switching Scenes to the Play-Start Menu

Discussion in 'Editor & General Support' started by ashkanaral, Feb 10, 2020.

  1. ashkanaral

    ashkanaral

    Joined:
    Sep 3, 2018
    Posts:
    22
    I am trying to add a game difficulty in Options menu. Then, when for example when clicked button "Easy" and go back to the Main Menu and hit play; the scene for "Easy" should begin. I am new at unity. I searched on Google and Unity.

    I used this script from youtube for switching scenes. However, how does it save. I am not sure on Dontdestroyonload because I need to switch the scene "Easy" unless I can save the button click.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class NextStagePlay : MonoBehaviour
    {
    public void LoadScene(int level)
    {
    Application.LoadLevel(level);
    }
    }
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Application.LoadLevel is deprecated. Not sure if it is even supported anymore. The current method is SceneManager.LoadScene or LoadSceneAsync.

    https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.LoadScene.html

    If you just need to save a difficulty level, I would just use a static variable. Then just choose the scene based on the value of the static variable. Maybe default it to easy. Optionally add saving the value to PlayerPrefs so your users don't need to select their difficulty level every time they launch your game.

    Code (csharp):
    1. public enum DifficultyLevel {Easy, Medium, Hard, Nightmare };
    2. public static DifficultyLevel CurrentDifficulty = DifficultyLevel.Easy;
    Code (csharp):
    1. public void LoadScene()
    2. {
    3.     switch (CurrentDifficulty)
    4.     {
    5.         case DifficultyLevel.Easy:
    6.             SceneManager.LoadScene("Easy");
    7.             break;
    8.         case DifficultyLevel.Medium:
    9.             SceneManager.LoadScene("Medium");
    10.             break;
    11.         case DifficultyLevel.Hard:
    12.             SceneManager.LoadScene("Hard");
    13.             break;
    14.         case DifficultyLevel.Nightmare:
    15.             SceneManager.LoadScene("Nightmare");
    16.             break;
    17.     }
    18. }
     
    JamesArndt and ashkanaral like this.
  3. ashkanaral

    ashkanaral

    Joined:
    Sep 3, 2018
    Posts:
    22
    Thank you! However, I put the script on the "Play" button in the Main Menu. The script does not show any onClick() or functions. I tested the script, and it does not work. I must probably have to do some other things to it?
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    I didn't write an entire script there :p

    With what I wrote you'd need to add the part about selecting the difficulty level at minimum. You could call that LoadScene() method from a button's OnClick (you'd manually add it to the OnClick event of the button in the inspector).
     
  5. newjerseyrunner

    newjerseyrunner

    Joined:
    Jul 20, 2017
    Posts:
    966
    I'm just putting out my opinion that I'm not sure why you would want different scenes for different difficulties. I would make everything use the same scene and just have slightly different setup.

    I do this by not actually having any enemies on my maps, I just have spawn points. Different spawn points are organized under different root objects, and the level's script knows which ones are for which difficulty. On level load, it simply destroys the spawn points that aren't needed for the difficulty level. Everything else is just real time changes to timing and variables in the AI.
     
    shekalo, Joe-Censored and ShilohGames like this.
  6. ashkanaral

    ashkanaral

    Joined:
    Sep 3, 2018
    Posts:
    22
    This is what I got so far. It still does not work or I am not doing it correctly. The play button I used this code.

    Code (CSharp):
    1. public void DiffLevel()
    2.     {
    3.         int i = PlayerPrefs.GetInt("SceneDiff");
    4.         switch (CurrentDifficulty)
    5.         {
    6.             case DifficultyLevel.Easy:
    7.                SceneManager.LoadScene(i);
    8.                 break;
    --------------------------------------------------------------------------------------------

    Then the Options Menu I used the event public to set the int inside event in inspector

    Code (CSharp):
    1. public class SaveDiffLevel : MonoBehaviour
    2. {
    3.     public int sceneNum;
    4.     public void UseLevel()
    5.     {
    6.         PlayerPrefs.GetInt("SceneDiff", sceneNum);
    7.         PlayerPrefs.Save();
    8.     }
    9. }
     
  7. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    ashkanaral likes this.
  8. ashkanaral

    ashkanaral

    Joined:
    Sep 3, 2018
    Posts:
    22
    Thank you! That solved it!
     
    Joe-Censored likes this.
  9. iriegusto

    iriegusto

    Joined:
    Nov 21, 2020
    Posts:
    20
    This is exactly what I'm trying to do. Can you please share the part of your level load code that destroys the unneeded spawn points?
     
  10. iriegusto

    iriegusto

    Joined:
    Nov 21, 2020
    Posts:
    20
    Here is what I have so far (working for enemy spawn)--just don't know how to either destroy unneeded spawn points for easier Difficulty settings buttons in the Options menu OR
    (maybe simpler) how to code in my Difficulty settings (I want easy, medium, hard selection preserved throughout all levels) so that public int spawnrate will generate a box in the Inspector that actually allows me to alter spawn speed by attaching the code to each Difficulty button in the Options menu

    Code (CSharp):
    1. public class RandomSpawnC : MonoBehaviour
    2. {
    3.     //prefabs to instantiate
    4.     public GameObject prefab1, prefab2, prefab3, prefab4;
    5.    
    6.     //spawn prefabs once per 4 seconds
    7.     public float spawnRate = 4f;
    8.    
    9.     //variable to set next spawn time
    10.     private float nextSpawn = 0f;
    11.    
    12.     //variable to contain random value
    13.     private int whatToSpawn;
    14.    
    15.     //update is called once per frame
    16.     void Update()
    17.     {
    18.         if (Time.time > nextSpawn) { //if time has come
    19.             whatToSpawn = Random.Range(1, 6); // define random value between 1 and 4 (5 is exclusive)
    20.             Debug.Log(whatToSpawn); //display its value in console
    21.            
    22.             //instantiate a prefab depending on random value
    23.             switch (whatToSpawn) {
    24.                 case 1:
    25.                     Instantiate(prefab1, transform.position, Quaternion.identity);
    26.                     break;
    27.                 case 2:
    28.                     Instantiate(prefab2, transform.position, Quaternion.identity);
    29.                     break;
    30.                 case 3:
    31.                     Instantiate(prefab3, transform.position, Quaternion.identity);
    32.                     break;
    33.                 case 4: Instantiate(prefab4, transform.position, Quaternion.identity);
    34.                     break;
    35.                
    36.             }
    37.             //set next spawn time
    38.             nextSpawn = Time.time + spawnRate;
    39.             // Start is called before the first frame update
    40.    
     
  11. MDADigital

    MDADigital

    Joined:
    Apr 18, 2020
    Posts:
    2,198
    You need to learn basic programming before you try to make a game. For example you need to learn how to use collections.
     
    iriegusto likes this.
  12. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,566
    Why bother DESTROYING spawnpoints, when spawnpoint itself can check current difficulty and then simply spawn nothing if the difficulty is not high enough?
     
    iriegusto likes this.
  13. newjerseyrunner

    newjerseyrunner

    Joined:
    Jul 20, 2017
    Posts:
    966
    I’m not at my computer, but it’s very simple. I have a script that’s attached to the level itself, so when it loads, its one of the only objects actually spawned in. All the enemies and weapons are in groups based on their difficulty. The script has a list of objects that it knows are supposed to be turned on at the selected difficulty.

    on load, if I’m on the hardest difficulty, it’ll turn on everything. If I’m on the easiest difficulty, it will enable only the stuff in the easy list, and delete everything else.

    This does mean that for the very first frame, there is nothing spawned in. I hide this by fading into each level.
     
    iriegusto likes this.