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 werneroi · Apr 22, 2012 at 10:51 PM · javascriptif-statements

really strange thing with "if" statment not working well

i am attaching a scrit in Js which suppose to decide if the new score is a high score or not. at the end of the script I have and "if" statment that suppose to check if the value of the var "hightime" is equal or not to the value of "ctime". even though it is showing trhough the Debug.Log that the values are equal, it is behaving as if they are not. i am also attaching a screen shot of the debug log where it shows that the valuse are the same but it prints some times "Old Highscore" - which suppose to happen only when the valuse are not the same. I have no idea why, or what to do to fix this. any idea will be great!

 #pragma strict
 var textfield:GUIText;
 var hightime : float;
 var levelnum : int;
 var scoreFiles : TextAsset;
 var thisTime : TIMER1;
 var index = 0;
 var scorestring : String;

 levelnum = GameObject.Find("LevelCounter(Clone)").GetComponent(levelCounter).levelNum;
  
 function Start () 
 {
 textfield = GetComponent(GUIText);
 scorestring = PlayerPrefs.GetString("scoreArry");
 var score : Array = scorestring.Split (","[0]);    
 var j = score.length;
 var time = GameObject.Find("txt-time").GetComponent(TIMER1);
 var ctime = time.levelTimer;
      
     for (var i = 0; i<j; i++)
     {
        index = i;
        if (index+1 == levelnum)
        {
           var thisTime : String = score[index];
           hightime = parseFloat(thisTime);
           textfield.text = "  = " + hightime.ToString("F2");
           Debug.Log ("1. " + score[index]);
           Debug.Log ("2. " + hightime);
           Debug.Log ("3. " + ctime);
           if (   hightime == ctime)
           {
              Debug.Log ("New Highscore");
              var _bestimeNew = UIButton.create ("GFXGold.png", "GFXGold.png" ,0,0);
              _bestimeNew.positionFromBottom(0.35,-.15);
           }
           else if (  hightime < ctime)
           {
           Debug.Log ("Old Highscore");
           var _bestime = UIButton.create ("GFXGoldBW.png", "GFXGoldBW.png" ,0,0);
           _bestime.positionFromBottom(0.35,-.15);
            }
         }
      }
  }

alt text

Screen shot 2012-04-23 at 1.26.32 AM.png (43.3 kB)
Comment
Add comment · Show 2
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 syclamoth · Apr 22, 2012 at 11:43 PM 0
Share

Add static typing to everything, and then see where you are. I suspect that the problem happens because you are comparing a string with a float- the float that evaluates to '3.14' is not the same as the string "3.14".

avatar image Kleptomaniac · Apr 23, 2012 at 07:31 AM 0
Share

Also, you have declared a variable named time. Time is already a built-in class. It ain't generally a good idea to be declaring your variables the same as class, because this can cause ambiguity and thus, screw things around in your code. Try calling it something else.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by fafase · Apr 23, 2012 at 08:33 AM

Am I wrong or you are doing all this in the start fct?

You are only checking once at the beginning but then in the game nothing of it is happening.

But I don't understand the whole thing, what would be the point to check at start the highscore? you'd rather check it at the end, seems pretty logic to me.

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

6 People are following this question.

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

Related Questions

Why does if(levelToLoad ==null) not work? 2 Answers

If/else statements working one way, and not the other 0 Answers

Setting Scroll View Width GUILayout 1 Answer

not sure whats happened :S 1 Answer

Code not entering if statement. 1 Answer


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