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.
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.
This error I’m getting continuously.
Error CS0201 Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement
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.
Have you already compared your code to the Lecture Project Changes which can be found in the Resources of this lecture?
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.
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);
}
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.