Functions:
1)Calculates result, takes in text input and this compares result to answer giving correct or incorrect.
2)Points: 10 awarded for correct, -30 for incorrect.
Problem:
This works flawlessly APART FROM the first answer which it always states is incorrect.
I’ve tried defining the result outside of the check, same thing.
Code:
public void CHECKANSWER() {
int RESULT = POWERS[0] + POWERS[1];
int ANSWERGIVEN = int.Parse(INPUT.text);
Debug.Log(ANSWERGIVEN);
if (ANSWERGIVEN == RESULT)
{
Debug.Log("CORRECT");
GameBoard.GetComponent<GameBoard>().UpdateScore(10);
MAKEQUESTION();
}
if (ANSWERGIVEN != RESULT)
{
Debug.Log("INCORRECT");
GameBoard.GetComponent<GameBoard>().UpdateScore(-30);
MAKEQUESTION();
}
}
I doubt the problem is in the code you posted. If it does not work the first time, but works after that, then something is not initialized to a value you expect in the first run. Go through your code step by step and figure out what’s different between the first and second question. You are already printing “ANSWERGIVEN”, you could also print “RESULT” to see why they are different, thus resulting in an incorrect evaluation.
A few tips on code quality;
The answer can only be equal to the result, or not equal. Thus you can simply use an if-else statement, instead of two different if’s. This makes little difference as of now, but if you make more complex statements, it would become harder to maintain the way you wrote it now.
If something happens both in the “if” and the “else” part, then it’s completely unrelated and can just be added below them, since it should always run anyways. Thus your MAKEQUESTION() method could be called below the if’s.
Following C# coding conventions helps to make your code more readable and maintainable and also uniform with the code of other programmers. You are currently writing your variable and method names using CAPSLOCK. You should rather use camelCase for variables, and UpperCamelCase for method, class and property names. That way you can easily differentiate them, write code that’s more readable for others, and write code that’s uniform with Unity and C# themselves.
Thank you Yoeki for your helpful and polite response.
The “step by step” helped me stop focusing JUST on this particular code and think more broadly, what I took away from that I will use again when stuck.
Re code quality:
IF-ELSE: I was being lazy with the double if because of the “makes little difference as of now” but agree that your option is better for this because it builds better general practice/methods, I’ll make the adjustment.
CAPS: Re camelCase and readability, the reasoning is dislike the look of camelCase which is why I use caps. 99% of the time I am the only one reading my code. It’s not just code either, it (caps overuse) follows through into everything from gameobject names in unity, to folders…to every document name on my computer. Just an odd personal preference, but I do agree when showing other’s my own work having what they are used to and probably like (camelCase) would be better, else it may be a distraction and even irritating for them. (GameBoard and UpdateScore were from a script written by another, hence the inconsistency).
You are free to name your stuff however you want. The inconsistencies would personally annoy me tho, and even if you now think that you’ll be the only one reading your code, that may change. For example, you may “unexpectedly” have huge success with your game and chose to hire someone to work on it with you. You’ll then have to refactor your entire project, which is annoying, takes time, probably introduces mistakes / bugs in the process and so on. Basically the same reasons as for why you should have english variable names and so on.
But then again, as long as you are doing it intentionally, i’m not stopping you. Most people just dont know better.
Yoreki, thank you for your initial help I do mean that.
All code is now fully functional…and of course, in full caps.
I am fully aware of the hypothetical situation you describe, it simply doesn’t concern me.