- Home /
[Solved] Boolean is true and flase every frame
Hi every one I'm trying to disable my (aiming) joystick while the player enter a door trigger and change the fire button to door opening button using a boolean the boolean is not valued and changes with a method in another script.
The problem is canAim (boolean) is showing both true and false every frame using debug.log(canAim)
Here is the code related to joystick and boolean
private void Update()
{
Debug.Log(canAim);
int i = 0;
while (i < Input.touchCount)
{
if (Input.GetTouch(i).position.x > Screen.width / 2 && canAim)
{
myTouch = Input.GetTouch(i);
myTouchPosition = myTouch.position;
touchID = myTouch.fingerId;
touchID2 = myTouch.fingerId;
isAiming = true;
}
i++
}
and this is the code in fire button script:
public void doorActionEnter()
{
GetComponent<Image>().sprite = doorSprite;
door = selectedDoor.GetComponent<door>();
doordetected = true;
joystick.canAim = false;
}
public void doorActionExit()
{
GetComponent<Image>().sprite = fireSprite;
doordetected = false;
joystick.canAim = true;
}
and this is the code in player script:
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.CompareTag("door"))
{
firebtn.selectedDoor = collision.gameObject;
firebtn.doorActionEnter();
}
}
private void OnTriggerExit2D(Collider2D collision)
{
if (collision.CompareTag("door"))
{
firebtn.doorActionExit();
}
}
I tried to make boolean in fire button script to reference it in joystick script. It works and all problem get solved but i get a null reference to fire button script too.
I dont understand. could any one help me out?
Unfortunately not. Every thing works fine. But as soon as the player leaves door trigger area canAim (boolean) should return true but it returns both true and false. Definitely something makes a loop but I can't find this in my code and I'm wondering this.
@reza74vi Boolean can only be assigned true OR false. It cannot be set to both true AND false at the same time. $$anonymous$$y guess is that you added the same script twice somewhere, so Debug.Log(canAim) is called twice every frame.
Answer by reza74vi · Apr 18, 2020 at 06:08 AM
This is a complex UI so this might be a little bit confusing. I tried to make a boolean in fire button script:
public bool canAim2;
private void Update()
{
if (doordetected) canAim2 = false;
else canAim2 = true;
}
and use this in joy stick script:
public firebtn firebtn;
private void Start()
{
firebtn = GameObject.Find("fireBtn").GetComponent<firebtn>();
}
private void Update()
{
canAim = firebtn.canAim2;
}
I tried to reference to fire button script in inspector window but it seemed to send me null reference so I tried to do it in script as you can see and now every thing is working fine. But I'm still wondering why the first solution did not worked out
Answer by Honorsoft · Apr 17, 2020 at 02:12 PM
in the DebugLog function, you wrote Debug.Log(canAim);
instead use:
Debug.Log(joystick.canAim);
(you still need to put the bool's "address")
But this script is joystick script already, did I get it wrong?
Oh, ok, I see, the first script is the joystick
script. So joystick.canAim
would only be necessary when referencing it from another script or object.
It would probably help to for us to see the whole scripts, even the part where you declare the variables. If I am not mistaken, you would have to initially declare the bool canAim
as true when it is first created, because you should be able to aim from the start.
It can be difficult to deter$$anonymous$$e the problem without seeing the whole code, because the error could be caused by a part that you haven't shown. SpartanTheDev is right though, you should check the tags and make sure everything is set up properly.
One surefire way to find out the problem is to look at your code line by line and deter$$anonymous$$e exactly what the code is doing step by step.
You project is a 2D project built for mobile devices obviously (hence the Collider2D and GetTouch), but one thing I don't understand is that you say the canAim
bool is returning both true and false, but a bool can only be either true or false, not both.
One thing that might help is to make your code as simple as posssible, for example, if the player is the one using the joystick and the fire button, then have the joystick and fire button code included in the player script. The fire button code should already be included with the rest of your input code in the joystick script. With all those scripts accessing each other it can be really hard to debug if something's not working.
One last thing, there are other things in your scripts that might cause problems, such as your lines: touchID = myTouch.fingerId; touchID2 = myTouch.fingerId; Those two different variables are redundant since they are assigning the same value.
thanks for answer @Honorsoft , i should say that canAim
is just declared in joystick script and all changes happens through a reference in fire button script. There are two floating joystick, one for move and one for ai$$anonymous$$g and only one of them is supposed to work a the same time. buttons and joystick are visible if only necessary or a touch detected. canAIm
only changes only on door trigger enter and exit but something makes a loop which makes this be true and false in a frame even after leaving door trigger area completely, but i found another way to fix this which I will say soon.
Sorry I wasn't more help. I even tried to build a version of your project using what code you posted to try to figure out your problem but I was missing parts. However, if you need any help feel free to ask.
Answer by SpartianTheDev · Apr 17, 2020 at 02:48 PM
You should check tags that door has "door" tag or player can interact door's collider in
Project settings >> Physics2DTry if you are not interacting with multiple doors:
private void OnTriggerExit2D(Collider2D collision) { if (collision.CompareTag("door")) { firebtn.selectedDoor = null; firebtn.doorActionExit(); } }
and debug selected door or use it instead of canAim
thanks for answer @SpartianTheDev but door opening system works fine and the door opening button disappear after leaving door trigger area. the only problem is boolean loops true and false every frame. Any how I found a trick to solve this that I'll answer.