• 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 Nik-60 · Jul 03, 2015 at 01:58 PM · triggerboolmovetowardstrue

Bool is always true

I'm working on a script that triggers a Drill when the Player enter's the trigger zone of the Drill gameobject. When triggered the drill is supposed to come down and go back up.

Here's the code

 using UnityEngine;
 using System.Collections;
 
 public class Drill_Small : MonoBehaviour {
 
     public Transform Initial;
     public Transform Final;
     public bool Trigger = false;
     public bool Down;
     public bool Up;
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         if(transform.position == Initial.position) {
             Down = true;
             Up = false;
         }
         if(transform.position == Final.position) {
             Up = true;
             Down = false;
         }
         if (Trigger == true) {
             if (Down = true) {
                 transform.position = Vector3.MoveTowards (transform.position, Final.position, 5f * Time.deltaTime);
             }
             if (Up = true) {
                 transform.position = Vector3.MoveTowards (transform.position, Initial.position, 2.5f * Time.deltaTime);
             }
         }
     }
     
     void OnTriggerEnter (Collider col){
         if (col.gameObject.tag == "Player") {
             Trigger = true;
         }
     }
 }


Once triggered, The Drill gameobject comes down, But is not going back, When i checked in the inspector the Down boolean is always true, never goes back to false, And i cant set it to false even in the inspector. I don't know what's wrong with the code. Someone please help me.

Regards,Nik-60

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

2 Replies

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

Answer by malkere · Jul 03, 2015 at 02:02 PM

if (Down = true)

will set down to true, you need ==

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
1
Wiki

Answer by starikcetin · Jul 03, 2015 at 02:05 PM

Actually, i can't understand how if (Down = true) works. It must give an error says the thing you do is not a bool. Correction is if (Down == true)

Comment
Add comment · Show 11 · 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 malkere · Jul 03, 2015 at 02:08 PM 0
Share

public bool Down;

it is a bool?

avatar image Nik-60 · Jul 03, 2015 at 03:30 PM 0
Share

Thank you Guys, Its working now. Was wrestling with this piece of code for about 4 hours. You guys made my day.

avatar image meat5000 ♦ · Jul 03, 2015 at 03:50 PM 1
Share

I could be wrong @s.ta.c but in the case you described I would imagine the if statement reads true if the assignment was successful. This is why its always true.

I could be talking bollocks.

avatar image meat5000 ♦ · Jul 03, 2015 at 04:09 PM 1
Share

No bollocks. Chicken-skin free gums.

I get a Warning not an error

Assets/Scripts/BoolTest.cs(16,17): warning CS0665: Assignment in conditional expression is always constant; did you mean to use == ins$$anonymous$$d of = ?

and the statement always evaluates true as well as the variable having been changed to true.

Note this only works for bools it seems as changing to an int for example will make it throw an error "Cant convert int to bool".

So somewhere I'm right and somewhere I'm wrong.

@Bunny83, @Eric5h5, @tanoshimi, @AnyoneWithABetterUnderstandingOfProgram$$anonymous$$gThan$$anonymous$$e could you clear this up for us please?

 using UnityEngine;
 using System.Collections;
 
 public class BoolTest : $$anonymous$$onoBehaviour {
 
     public bool myTest = false;
 
     void Update ()
     {
         if(myTest = true)
         {
             Debug.Log("If statement evaluated true");
             Debug.Log(myTest);
         }
         else
         {
             Debug.Log("If statement evaluated false");
         }
     }
 }

avatar image _Gkxd · Jul 04, 2015 at 06:25 PM 1
Share

From https://msdn.microsoft.com/en-us/library/sbkb459w.aspx

The assignment operator (=) stores the value of its right-hand operand in the storage location, property, or indexer denoted by its left-hand operand and returns the value as its result.

Assignment statements return the thing that has been assigned. Hence a = true will return true. This is why things like a = b = c = d = true works.

You can also do things like a = !(b = c = !(d = true)), though that makes code hard to read. This is equivalent to saying

 d = true;
 c = false;
 b = false;
 a = true;
Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Animator bool = true on TriggerEnter? 0 Answers

Shoot single fireball through animation event 2 Answers

MoveTowards doesn't work after changing tag. 2 Answers

How to add WAKE_UP animation before the character enters IDLE state? 1 Answer

How to start animation after object is destroyed ? 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