SuperNoob here. As i coded the game along you guys, tried to do something a little bit differently, but i failed (doesn’t work) and i don’t know why:
Basically wanted the game to ask you if you wanted to play again, so i created a function (New match):
void NewMatch() // Not working don't know why
{
Debug.Log("Press Space To Play Again!");
if (Input.GetKeyDown(KeyCode.Space))
{
start_game();
}
}
and called it on the last else if clause of the update() function, like so:
else if (Input.GetKeyDown(KeyCode.Return))
{
Debug.Log("Guessed it!");
Debug.Log("I AM A GENIUS");
NewMatch();
}
Could not, for the life of me understand why the game got fine to the "Press Space to play again part, but would not execute anything in the NewMatch() if statement, not even a debug log. Thanks for any answers, if you need me to post the whole code just ask ^^ (About 75 lines of code)
If “Guessed it!” appears, NewMatch() should get called, too. Make sure the messages in your console are enabled. Disable Collapse and enable Clear on Play.
Could you share your entire code, please? I suspect that the if-conditions do not get checked each frame. If they don’t, the player will have to press space and return within the same frame. If you have a framerate of 60, this would require the player to press the keys within 1/60 second. A friction too slow, and the other if-condition will get evaluated to false.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class HelloWorld : MonoBehaviour
{
int max_num;
int min_num;
int guess;
// Start is called before the first frame update
void Start()
{
start_game();
}
void start_game()
{
max_num = 1000;
min_num = 1;
guess = 500;
Debug.Log("Welcome to Number Wizard, yo");
Debug.Log("Think of a number!");
Debug.Log("Minimum:" + min_num);
Debug.Log("Maximum:" + max_num);
Debug.Log("Tell me if your number is higher or lower than: ");
Debug.Log("Push up = The number you thought is Higher");
Debug.Log("Push Down = The number you thought is Lower");
Debug.Log("Enter = CORRECT!");
Debug.Log(guess);
max_num = max_num +1;
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.UpArrow))
{
Debug.Log("Higher!");
min_num = guess;
NextGuess();
}
else if (Input.GetKeyDown(KeyCode.DownArrow))
{
Debug.Log("Lower!");
max_num = guess;
NextGuess();
}
else if (Input.GetKeyDown(KeyCode.Return))
{
Debug.Log("Guessed it!");
Debug.Log("I AM A GENIUS");
NewMatch();
}
}
void NextGuess()
{
guess = (max_num+min_num) / 2;
Debug.Log(guess);
}
void NewMatch() // Not working don't know why
{
Debug.Log("Press Space To Play Again!");
if (Input.GetKeyDown(KeyCode.Space))
{
start_game();
}
}
}
Assuming you press the Return key, the else if block gets executed. NewMatch gets invoked, the if inside that method gets evaluated. This happens within milliseconds. Unity does not wait for any user input. Are you sure that “Press Space To Play Again!” did not appear in your console? Or did it appear? If it appeared, your code is working as expected (by me).
Move the if-statement including its block from NewMatch() to Update() and test your game again.
You were right about the loop, as soon as i moved the if statement in update(), “Press space to play again” showed up, but pressing space did not work, suspect i still i have to understand well how the update loop works. So i declared a bool called ‘wizard’ and set it as false in start(). Then changed update like so:
void Update()
{
if (Input.GetKeyDown(KeyCode.UpArrow))
{
Debug.Log("Higher!");
min_num = guess;
NextGuess();
}
else if (Input.GetKeyDown(KeyCode.DownArrow))
{
Debug.Log("Lower!");
max_num = guess;
NextGuess();
}
else if (Input.GetKeyDown(KeyCode.Return))
{
Debug.Log("Guessed it!");
Debug.Log("I AM A GENIUS");
Debug.Log("Press Space To Play Again!");
wizard = true;
}
else if ((Input.GetKeyDown(KeyCode.Space)) && wizard==true)
{
{
start_game();
}
}
}
The bool would have been the next step I would have suggested to you after you made the else-if work. I’m glad to see that you came up with the idea yourself.