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 AarnasS75 · Jul 19, 2020 at 09:47 AM · booleanbool

Sometimes the bool value is not changing

So I have this mini game, where I spawn two identical prefabs, make them children of GameObject called "WorkerSpawner" and then move that game object with both children to center, when it reaches the center, the line above them starts extending and when a mouse button is pressed or it reaches x axis limit it stops extending and drops down. with a gravity scale set to 3.

My problem is that when I check if the "WorkerSpawner" has arrived to the center, the "HaveWorkersArrived()" returns bool values correctly all the time, but "TouchInputOn" does not, it sometimes returns true, but most of the time it returns only false. Why ?


Spawner class:

 public bool HaveWorkersArrived()
     {
         if (transform.position != centerPosition)
         {
             liScript.TouchInputOn = false;
             return false;
         }
         else
         {
             liScript.TouchInputOn = true;
             return true;
         }
     }



Line class:

 public GameObject line;
 
 GameObject workerz;

 private SpawnWorkers workerSpawner;

 public bool TouchInputOn;

 private void Start()
 {
     rb = GetComponent<Rigidbody2D>();
     workerz = GameObject.FindGameObjectWithTag("WorkerSpawner");
     workerSpawner = workerz.GetComponent<SpawnWorkers>();
 }
 private void Update()
 {
     
     if (TouchInputOn == true && workerSpawner.HaveWorkersArrived() == true)
     {
         line.transform.localScale = new Vector3(line.transform.localScale.x + 0.2f * speed, line.transform.localScale.y, 0);

         if (Input.GetButtonDown("Fire1") || line.transform.localScale.x >= 1f)
         {
             rb.gravityScale = 3f;
             TouchInputOn = false;
         }
     }
 }
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 Compii · Jul 19, 2020 at 10:50 AM

So the way that the if function works in C# is that when you use if with && like you did,

 if (TouchInputOn == true && workerSpawner.HaveWorkersArrived() == true)

it will first check if TouchInputOn is true. If it isnt, it wont even check HAveWorkersArrived(). However, TouchInputOn only gets set to true after executing HaveWorkersArrived(). So you could fix this by just changing the order of the if statement:

 if (workerSpawner.HaveWorkersArrived() == true && TouchInputOn == true)

So now I want to give you some more tips how you could improve your code. When you use bools in if statements you can just write the variable without == true because it does the same:

  if (workerSpawner.HaveWorkersArrived() && TouchInputOn)

That does the same as the Code above. If you check for false, you can just use "!" which basically inverts the bool:

  if (!workerSpawner.HaveWorkersArrived()  && !TouchInputOn)

Furthermore you check if touchInputOn and HaveWOrkersArrived() are true, but since you set TouchInputOn in HaveWorkersArrived() to the same value as it returns you dont need to check TouchInputOn too because it'll always be the same. You can just do:

  if (workerSpawner.HaveWorkersArrived())


Hope everything now works for you!

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 AarnasS75 · Jul 21, 2020 at 04:16 PM 0
Share

I tried changing the order as well, it still doesn't work. Also if I only leave "if(workerSpawner.HaveWorkersArrived())" it doesn't stop line from extending when it drops down.

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

134 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How Do I check if a Bool was True a few moments ago 2 Answers

How to change a Bool for only a single prefab GameObject? 1 Answer

Why is this bool auto turning off? 0 Answers

my button dosent work, became a light switch instead of a normal button (like a bell) 1 Answer

Boolean and input relation 0 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