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
0
Question by soand · Jul 21, 2014 at 09:23 AM · 2dbackgroundtransitionboolstatement

why does if statement ignore bool?

Hey guys i am trying to make a background in a 2D game which slowly scrolls but i am making a transition to enter a new world but it seems like unity just ignores my bool in the if statement?? anyone know how come?

the script is attached to two different pictures which then scrolls

     float moveSpeed = -0.02f;
     float World2 = 20f;
     float maxHeight = 0f;
     bool transition = false;
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
 
         transform.Translate (0, moveSpeed, 0);
 
         maxHeight = renderer.bounds.size.y;
 
         Vector3 pos = transform.position;
 
         if (-maxHeight >= pos.y) {
             pos.y = +maxHeight + moveSpeed;
             transform.position = pos;
         }
 
         if (Score.score >= World2) {
 
             if (pos.y >= 11.42f && transition == false){
                     Debug.Log ("HALLLO!!!!!");
                     gameObject.GetComponent<SpriteRenderer> ().sprite = Resources.Load("World2Trans", typeof(Sprite)) as Sprite;
                     
                     Vector3 scale = transform.localScale;
                     scale.y = 1.01f;
                     transform.localScale = scale;
 
                 transition = true;
             }
 
             if (pos.y >= 11.42f && transition == true){
                 gameObject.GetComponent<SpriteRenderer> ().sprite = Resources.Load("World2", typeof (Sprite)) as Sprite;
             }
         }
 
 
 
     }
 }

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Tehnique · Jul 21, 2014 at 09:25 AM

The "&&" operator short-circuits. That means that if the first expression is False, it does not evaluate the second. Same happens to "||" (if first expression is True, second is not evaluated). More here.

If you really want to evaluate both expressions, you can use &. Although, that makes no sense in your case (it's better suited if your second condition also runs some code you want to run regardless of the logical outcome).

Also, I'd refactor your code like this:

     if (Score.score >= World2 && pos.y >= 11.42f)
     {
                 if (!transition)
                 {
                         Debug.Log ("HALLLO!!!!!");
                         gameObject.GetComponent<SpriteRenderer> ().sprite = Resources.Load("World2Trans", typeof(Sprite)) as Sprite;
      
                         Vector3 scale = transform.localScale;
                         scale.y = 1.01f;
                         transform.localScale = scale;
      
                     transition = true;
                 }
                 else
                 {
                     gameObject.GetComponent<SpriteRenderer> ().sprite = Resources.Load("World2", typeof (Sprite)) as Sprite;
                 }
 }

Also, you have no code that resets "transition" once it's been set to true. Maybe you should look into that, too.

Comment
Add comment · Show 1 · 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 soand · Jul 21, 2014 at 12:30 PM 0
Share

Thank you for your answer! i realised that with two if statements it just went trough the first sprite and in the same frame run the second if, so i made an else if statement which sort of worked, but then the transition sprite i made, came twice. Then i realized that was because it was two different scripts. i then made the bool public, and made the script check for the bool on the other script and now it works :)

yes i will certainly look into making the bool false again, but after 2 hours not knowing what to do i just had to finish the transition logic before i cleaned up the code properly :) thanks again!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to hide game window but show sprite? 0 Answers

[C#] [2D] Animation mostly not working 1 Answer

endless 2D background in Javascript 0 Answers

Does disabling graphics game objects prevent them from being affected by scripts? 0 Answers

how to instantiate multiple 2d background? 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