- Home /
The if does not work?
The debug log does not output when the two variables are the same. //copyright 2013 #pragma strict
var cake : Texture;
var sugar : String = "sugar";
var jam : String = "jam";
var flour : String = "flour";
var eggs : String = "eggs";
var milk : String = "milk";
var water : String = "water";
var sugar2 = "";
var jam2 = "";
var flour2 = "";
var eggs2 = "";
var milk2 = "";
var water2 = "";
function Start () {
}
function Awake() {
var sugar2 = Random.Range(0, 100);
var jam2 = Random.Range(0, 100);
var flour2 = Random.Range(0, 100);
var eggs2 = Random.Range(0, 100);
var milk2 = Random.Range(0, 100);
var water2 = Random.Range(0, 100);
Debug.Log(sugar2);
}
function Update () {
}
function check() {
if(sugar == sugar2){
Debug.Log("sugar is right");
}
else{
Debug.Log(sugar);
}
if(jam2 == jam){
Debug.Log("jam is right");
}
if(flour2 == flour){
Debug.Log("flour is right");
}
if(eggs2 == eggs){
Debug.Log("eggs is right");
}
if(milk2 == milk){
Debug.Log("milk is right");
}
if(water2 == water){
Debug.Log("water is right");
}
}
function OnGUI() {
GUI.Label (Rect (Screen.width / 2 - 60, 80, 140, 80), cake);
GUI.Label (Rect (Screen.width / 2 - 50, 260, 100, 30), "Jam 0-100:");
jam = GUI.TextField (Rect (Screen.width / 2 + 50, 260, 100, 30), jam);
GUI.Label (Rect (Screen.width / 2 - 50, 300, 100, 30), "Sugar 0-100:");
sugar = GUI.TextField (Rect (Screen.width / 2 + 50, 300, 100, 30), sugar);
GUI.Label (Rect (Screen.width / 2 - 50, 340, 100, 30), "Flour 0-100:");
flour = GUI.TextField (Rect (Screen.width / 2 + 50, 340, 100, 30), flour);
GUI.Label (Rect (Screen.width / 2 - 50, 380, 100, 40), "Eggs 0-100:");
eggs = GUI.TextField (Rect (Screen.width / 2 + 50, 380, 100, 30), eggs);
GUI.Label (Rect (Screen.width / 2 - 50, 420, 100, 30), "Milk 0-100:");
milk = GUI.TextField (Rect (Screen.width / 2 + 50, 420, 100, 30), milk);
GUI.Label (Rect (Screen.width / 2 - 50, 460, 100, 40), "Water 0-100:");
water = GUI.TextField (Rect (Screen.width / 2 + 50, 460, 100, 30), water);
if (GUI.Button (Rect (Screen.width / 2 - 60, 660, 100, 40), "Quit")) {
Application.Quit();
}
if (GUI.Button (Rect (Screen.width / 2 + 60, 660, 100, 40), "check")) {
check();
}
}
Correct, if does not work and has been formally removed from all computer program$$anonymous$$g language specifications; your logic is sound.
Don' even know what your code should do. Please reduce it down to something someone else wants to read. You too have to do some work to get help...
Answer by ArkaneX · Sep 04, 2013 at 07:49 PM
The problem is, that you have some variables declared globally, and then in Awake function you don't initialize them, but instead you create local variables with the same name. So global variables remain unchanged and have values set in inspector (default empty string).
You have to change your assignments in Awake from
var sugar2 = Random.Range(0, 100);
to
sugar2 = Random.Range(0, 100).ToString();
and so on...
Answer by tanoshimi · Sep 04, 2013 at 07:45 PM
I have no idea what you're intending to achieve, but I imagine the reason why your code is not doing what you think it should is because the scope, types and values of your variables are all over the place!
To start off, you have a global variable "jam", which is explicitly declared as a String with the value "jam":
var jam : String = "jam";
You then have another global variable "jam2", which is implicitly declared as an empty string:
var jam2 = "";
Note that, in the check() function, it is these two values that are being compared. They're not the same, so the Debug.Log is correctly no printing anything.
In the Awake() function you're then declaring a local variable, also named "jam2", and giving it an integer value:
var jam2 = Random.Range(0, 100);
And then in the OnGUI() function you're setting the global variable "jam" (which, you recall, was initialised as a string "jam") to be the edited value returned by the textfield:
jam = GUI.TextField (Rect (Screen.width / 2 + 50, 260, 100, 30), jam);
See the problem? Give your variables more descriptive names and think about the scope in which they're used and problems like this are resolved pretty easily.
Your answer
Follow this Question
Related Questions
Extract number from string? 3 Answers
BCE0022: Cannot convert 'String' to 'int'. 0 Answers
GetHashCode() questions/Turning a text string into an int 1 Answer
int to string, weird string value? 1 Answer
'Cannot convert int to String' 2 Answers