Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
-1
Question by jpc133 · Sep 04, 2013 at 06:56 PM · stringintif

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();        
     }
                             
 }
Comment
Add comment · Show 13
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image getyour411 · Sep 04, 2013 at 06:59 PM 2
Share

Correct, if does not work and has been formally removed from all computer program$$anonymous$$g language specifications; your logic is sound.

avatar image jpc133 · Sep 04, 2013 at 07:12 PM 0
Share

what ever you say!

avatar image jpc133 · Sep 04, 2013 at 07:14 PM 0
Share

The Problem is line 34 to 40

avatar image fherbst · Sep 04, 2013 at 07:17 PM 0
Share

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

avatar image DaveA · Sep 04, 2013 at 07:18 PM 0
Share

Have you set a breakpoint on line 34?

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges