hehe, hi
Your description of the concept is a good one, I would add maybe one step in between the clicking of the button and the loading of the game scene, this would be where we store the difficulty and persist it so that we can then access it when the game scene loads.
The following is a way of achieving what you want, there will be others also
To keep things tidy, we will create a Difficulty.cs script;
public static class Difficulty
{
}
This class will store the player selection after they have clicked a button.
We can use an enum to represent our different difficulty levels, you will be familiar with the use of these from the Text101 game (States
etc).
We will also need to add a variable to hold the stored selection and provide a way of both getting and setting it. Our variable will be static so that it belongs to our class, not an instance of our class, and can persist.
public enum DifficultyLevel : int { Easy = 100, Medium = 1000, Hard = 10000 };
public static class Difficulty
{
private static DifficultyLevel _level;
public static DifficultyLevel Level
{
get { return _level; }
set { _level = value; }
}
}
In the above we;
-
define our enum called DifficultyLevel
- we specify that its underlying type will be
int
(this is the default in C# anyway)
- we add our constants to it, Easy, Medium, Hard
- as an added benefit, we can specify the integer value for each of these
-
define a variable called _level
- setting it’s type to
DifficultyLevel
(our enum)
- setting its scope to be private
- and make it static
-
define a property called Level
- setting it’s type to
DifficultyLevel
(our enum)
- setting its scope to be public
- and make it static
- we add a getter, which returns the value from
_level
- we add a setter, which sets the value of
_level
Next, we need to think about the interaction between the buttons and the game. To me, it doesn’t feel appropriate to have the interaction between the game objects and the Difficulty class, nor does it really feel appropriate for that interaction to happen within your NumberWizard class, as this is really the main game logic.
What we will do, at least for now, is create a new class called GameManager. This class will interact with our buttons, set the difficulty level for the game, and make a call to the LevelManager to load the game scene.
Note, there is somewhat of an overlap between the GameManager and NumberWizard classes, but for now, we will focus on creating the desired functionality and making it work with the existing code, you can alway consider refactoring afterwards.
using UnityEngine;
using UnityEngine.EventSystems;
public class GameManager : MonoBehaviour
{
}
Note the addition of the UnityEngine.EventSystems directive at the top of this script, this will allow us to use methods to interact with Events raised by objects.
We will need to add a method to this class which we can add to the OnClick
event for each of the buttons, we will build on this as we continue.
using UnityEngine;
using UnityEngine.EventSystems;
public class GameManager : MonoBehaviour
{
public void NewGame()
{
}
}
Now, with the scripts saved, return to the Unity editor and open your start scene;
We now need to return to our GameManager script and add the rest of the code.
We are going to add two more methods to this class and call them from NewGame
. Update the NewGame
method as follows;
public void NewGame()
{
SetDifficulty();
StartGame();
}
The SetDifficulty
method will do exactly that, it will determine, based on the name of the button that was clicked, which difficulty level the game should start in;
private void SetDifficulty()
{
GameObject button = EventSystem.current.currentSelectedGameObject;
if (button != null)
{
switch (button.name.ToUpper())
{
case "EASY":
{
Difficulty.Level = DifficultyLevel.Easy;
break;
}
case "MEDIUM":
{
Difficulty.Level = DifficultyLevel.Medium;
break;
}
case "HARD":
{
Difficulty.Level = DifficultyLevel.Hard;
break;
}
}
}
}
In the above;
- we get a reference to the selected game object, e.g. the button that was clicked
- just as a precaution, we check that this hasn’t returned as
null
- we use a
switch
statement to determine which button was clicked
- we uppercase the GameObject’s name and compare against upper-cased values
- this test is based on the name of the GameObject (not ideal, but it will work for now)
- in each of the cases;
- we set the static variable
_level
in our Difficulty class, via our property Level
, to equal the appropriate difficulty level from our DifficultyLevel
enum.
Important Note: The above method relies on your buttons being name, “Easy”, “Medium” and “Hard”, if they are not, either rename them, or, change the values in each of the case
blocks in the switch
statement above to match your button’s names.
The second method, StartGame
gets a reference to our LevelManager
and calls the LoadLevel
method, as your original button click(s) would have.
private void StartGame()
{
LevelManager levelManager = GameObject.FindObjectOfType<LevelManager>();
levelManager.LoadLevel("game");
}
Next we integrate the difficulty setting into your NumberWizard script, which is achieved by adding this Start
method;
private void Start()
{
min = 1;
max = (int)Difficulty.Level;
NextGuess();
}
We can also now remove the following, by just commenting them out initially;
/*
public void StartGame100(){
min = 1;
max = 100;
guess = 50;
guessText.text = guess.ToString();
max = max + 1;
maxGuess = 8;
}
public void StartGame1000(){
min = 1;
max = 1000;
guess = 500;
guessText.text = guess.ToString();
max = max + 1;
maxGuess = 9;
}
public void StartGame10000(){
min = 1;
max = 10000;
guess = 5000;
guessText.text = guess.ToString();
max = max + 1;
maxGuess = 10;
}
*/
the following lines can be removed also;
using System.Collections;
public Text test;
There is one final thing we need to do. You have been rather awesome an added a varying number of maximum allowed guesses, based on the difficulty level, so far, we haven’t implemented that. Let’s make a couple of changes so that we can provide that functionality too.
There are a number of places we could set this, but as the maximum number of allowed guesses relate to the difficulty level of the game, it makes sense to me to store this value in the same place, Difficulty.cs;
public enum DifficultyLevel : int { Easy = 100, Medium = 1000, Hard = 10000 };
public enum MaximumGuesses : int { Easy = 8, Medium = 9, Hard = 10 };
public static class Difficulty
{
private static DifficultyLevel _level;
private static MaximumGuesses _guesses;
public static DifficultyLevel Level
{
get { return _level; }
set { _level = value; }
}
public static MaximumGuesses MaximumGuesses
{
get { return _guesses; }
set { _guesses = value; }
}
}
In the above we additionally;
-
define an enum called MaximumGuesses
- we specify that its underlying type will be
int
(this is the default in C# anyway)
- we add our constants to it, Easy, Medium, Hard
- as an added benefit, we can specify the integer value for each of these
-
define a variable called _guesses
- setting it’s type to
MaximumGuesses
(our enum)
- setting its scope to be private
- and make it static
-
define a property called MaximumGuesses
- setting it’s type to
MaximumGuesses
(our enum)
- setting its scope to be public
- and make it static
- we add a getter, which returns the value from
_guesses
- we add a setter, which sets the value of
_guesses
Next we need to look at our GameManager script, as this is where we set the difficulty level based on the button click;
private void SetDifficulty()
{
GameObject button = EventSystem.current.currentSelectedGameObject;
if (button != null)
{
switch (button.name.ToUpper())
{
case "EASY":
{
Difficulty.Level = DifficultyLevel.Easy;
Difficulty.MaximumGuesses = MaximumGuesses.Easy;
break;
}
case "MEDIUM":
{
Difficulty.Level = DifficultyLevel.Medium;
Difficulty.MaximumGuesses = MaximumGuesses.Medium;
break;
}
case "HARD":
{
Difficulty.Level = DifficultyLevel.Hard;
Difficulty.MaximumGuesses = MaximumGuesses.Hard;
break;
}
}
}
}
In the above, we have added an extra line in each of the case
blocks which set our maximum guesses for each of the three difficulty levels.
We then open our NumberWizard script and update our Start
method as follows;
private void Start()
{
min = 1;
max = (int)Difficulty.Level;
maxGuesses = (int)Difficulty.MaximumGuesses;
NextGuess();
}
As you can see, we now set the local variable within NumberWizard to the value set in our Difficulty script.
So, that was a bit of a lengthy response - please take your time to work through it step by step and let me know how things turn out.
Hope this helps
ScriptFiles.zip (1.3 KB)