How to give player an option to start the game on their own?

I try to create a function that gives the player an option to start the game again by press a button?
But somehow, it does not work. What have I done wrong?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NumberWizard : MonoBehaviour
{
int max = 1000;
int min = 1;
int guess = 500;
// Start is called before the first frame update
void Start()
{
GameStart();
}
void GameStart()
{
max = 1000;
min = 1;
guess = Random.Range(min, max);
Debug.Log(“Welcome to number wizard, boys!!!”);
Debug.Log(“Pick your number”);
Debug.Log("Highest number is " + max);
Debug.Log("Lowest number is " + min);
Debug.Log("Tell me if your number is higher or lower than: " + guess);
Debug.Log(“Push Up = Higher, Push Down = Lower, Push Enter = Correct”);
max += 1;
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.UpArrow))
{
min = guess;
NextGuess();
}
else if (Input.GetKeyDown(KeyCode.DownArrow))
{
max = guess;
NextGuess();
}
else if (Input.GetKeyDown(KeyCode.Return))
{
Debug.Log(“Boy, you are breathtaking”);
Debug.Log(“Do you want to play again? Press Space to play again!”);
PlayAgain();
}
}
void NextGuess()
{
guess = (min + max) / 2;
Debug.Log("Is your number lower or higher than " + guess + “?”);
Debug.Log(“If correct, congratulation! If not, try again!”);
}
void PlayAgain()
{
if (Input.GetKeyDown(KeyCode.Space))
{
GameStart();
}
}
}

Hi,

Welcome to our community! :slight_smile:

In GameStart, you could write something like “Welcome to Number Wizard! Press Enter to start the game.”

In Update, you could add another if with KeyCode.Return. Inside the if-block, you add your instruction, which is currently in GameStart.

Also please feel free to ask our helpful community of students for advice in our official Discord chat.


This topic was automatically closed after 3 hours. New replies are no longer allowed.

Privacy & Terms