Unity 2D Course - Number wizard game - C# Function calling problem

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)

Hi Fralo87,

Welcome to cour community. :slight_smile:

Your code looks fine so far. Did you save it?

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.

Yes i did! What left me appalled is that unity console didn’t even throw errors at me :sweat_smile: The code just stops working at

Debug.Log("Press Space To Play Again!");

like the if statement was not even there :sweat_smile:

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.

Sure! Here it is, thanks for any help ^^

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();
        }
    }

}

Thank you. It seems that I’m right.

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();
           }
       }
                       
    }

It works ^^

Thank you for your help!

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. :slight_smile:

Is everything working now as intended/expected?

Yes! Thanks for all your help! Though it was not bad for my first day with C# XD

Next time, please don’t claim you were a supernoob because you obviously aren’t. Keep up the good work! :slight_smile:


See also:

Supernoob with C#, but have been pythoning since january^^

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms