- Home /
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
Answer by malkere · Jul 03, 2015 at 02:02 PM
if (Down = true)
will set down to true, you need ==
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)
Thank you Guys, Its working now. Was wrestling with this piece of code for about 4 hours. You guys made my day.
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.
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");
}
}
}
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;
Your answer
Follow this Question
Related Questions
OnTriggerEnter if else 2 Answers
Void OnTriggerEnter not working 1 Answer
How to do Multiple OnTriggerEnters 2 Answers
Interrupt a MoveTowards when triggering 2 Answers
VR Player enters trigger won't work 0 Answers