- Home /
True and False values with If and else
I'm trying to make a script were if the collider of one object tagged as "select" change the true and false values of a variable called "select". Here is the code:
function OnTriggerEnter (Col : Collider)
{
if(Col.tag == "Select")
{
select = true;
}
else
{
select = false;
}
}
When it isn't colliding with "select" should it not change back the false value? The collider tagged "select" is turned off and on useing the "e" key with this script:
enter code herefunction Update ()
{
if (Input.GetKey("e"))
{
GameObject.Find("Select").GetComponent(Collider).enabled = true;
}
else
{
GameObject.Find("Select").GetComponent(Collider).enabled = false;
}
}
Is the script above to blame for this problem? `
Answer by haim96 · Feb 16, 2014 at 08:20 PM
the if statement runs only when you have collision detection. if you turn off the collider there will be no collision and the if - else statement will never run so select will never return to be false.
first, i wouldn't touch the collider status since you will need it enabled... i guess. for the rest it depend on you needs, if they are simple you could adjust the object TAG to "selected" or "notSelected" but this will work well if you don't need to find the object by tag anywhere else. other method, is to store the status of the object on variable but it need a bit more coding.
Your answer
![](https://koobas.hobune.stream/wayback/20220613133731im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Why won't my if result to true? 1 Answer
Boolean values not behaving as expected. Please Help?! 0 Answers
Run a function aslong as a button is pressed 3 Answers
true and false wont work! 4 Answers
C# bool setting itself to true 1 Answer