Sir, I have a doubt in if & else statements and encapsulating

  1. You taught how to write if and else if statements. But, how to write an else statement. Whenever I am trying to write an else statement, it always shows some error messages. I’m not getting it how to do this.

  2. After encapsulating when we press enter, it starts from the StartGame functinon along with the statement we wrote what to say if player press enter. But i want when 1st time player press enter it only show the statement we wrote there("I am a genius ! "). How to do that.

Please help me.

Hi Sibam,

  1. What error messages do you get? Bear in mind that else always needs a preceding if or else-if, and the else-if needs a preceding else-if or if.
  2. What do you mean by “first time”? What is supposed to happen the second time?
  1. This error I’m getting continuously.
    Error CS0201 Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement

  2. In number wizard, if my guess is correct and the player press enters enter, first it should show the statement i put in ‘Debug.Log’. Then if the player again press any key the game should restart.

  1. Have you already compared your code to the Lecture Project Changes which can be found in the Resources of this lecture?
  2. This requires game states because the computer has no conception of “first time” or “second time”. Implementing game states is relatively easy. Once you fixed issue 1, you could proceed with learning about enums. Instead of “Importance” as seen in the example on the linked website, you name your enum GameState and define a few states.

Yes, I have compared with the github resources. There’s no else statement out there. But in case i need it anytime, I want to know how to use else statements.

Double click on the error message. To which line in your code does it refer? Maybe there is a superfluous semicolon behind an if-condition.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NumberWizard : MonoBehaviour
{
int max;
int min;
int guess;

// Start is called before the first frame update
void Start()
{

    StartGame();

}


void StartGame()
{

    max = 5000;
    min = 50;
    guess = 2525;


    Debug.Log("Jay Jagannath. Namaste");

    Debug.Log("Pick a number and wait for the magic to happen");

    Debug.Log("Highest number you can pick is: " + max);

    Debug.Log("Lowest number you can pick is: " + min);

    Debug.Log("It is higher or lower than " + guess);

    Debug.Log("Press up = Higher, Press down = Lower, Press Enter = Correct");

    max = max + 1;



}
// Update is called once per frame
void Update()
{

    if (Input.GetKeyDown(KeyCode.UpArrow))
    {
        Debug.Log("Higher");
        min = guess;
        NextGuess();

    }

    else if (Input.GetKeyDown(KeyCode.DownArrow))
    {
        Debug.Log("Lower");
        max = guess;
        NextGuess();
    }

    else if (Input.GetKeyDown(KeyCode.Return))
    {
        Debug.Log("Hereby the magic happened");
        StartGame();
    }

    else (StartGame);
}

void NextGuess()
{

    guess = (max + min) / 2;
    Debug.Log("It is higher or lower than " + guess);


}

}

Check the else statement here.

The problem is not the else. It is the code in the else-block. StartGame is a method. That’s not how you call methods. Check the code block of your Start method. There is an example of how to call a method.

Mam, I’m still not getting it. Can you please give me an example of else statement ?

else StartGame();

Alternatively:

else
{
    StartGame();
}

Thank you mam.

You’re welcome. :slight_smile:


See also:

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

Privacy & Terms