- Home /
Question by
Joshua10101 · Jul 22, 2017 at 12:58 PM ·
collisionmovementtriggerboolean
OnTriggerEnter2D not working with boolean
public int MoveSize;
bool CantMoveDown = false;
bool Touching = false;
void Update () {
if (Input.GetKeyDown(KeyCode.DownArrow)){
if (CantMoveDown = false) {
transform.position = new Vector2(transform.position.x, transform.position.y - MoveSize);
}
if (Touching = true){
CantMoveDown = true;
print ("Collided"); //Just for testing
}
}
void OnTriggerEnter2D (Collider2D trigger){
Touching = true;
}
void OnTriggerExit2D (Collider2D trigger){
Touching = false;
}
This is part of my movement script. What is supposed to happen is if the sprite this script is attached to moves downwards and is adjacent to another sprite (With both 'Is Trigger' checked and a fitting collider), it should not be able to move downwards anymore until it doesn't detect the sprite below.
Instead, I cannot move down at all and the console is printing "Collided". Am I using the OnTriggerEnter/Exit right? The script makes sense to me but is it incorrect?
Any help would be greatly appreciated! Thanks!
Comment
Best Answer
Answer by FlaSh-G · Jul 23, 2017 at 09:34 PM
if(Touching = true) {
This is not a comparison, but an assignment.
The correct line would be
if(Touching == true) {
or, because == true
can be omitted
if(Touching) {
Your answer
![](https://koobas.hobune.stream/wayback/20220612130851im_/https://answers.unity.com/themes/thub/images/avi.jpg)