Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 reza74vi · Apr 16, 2020 at 02:57 PM · c#androidscripting problembooleanjoystick

[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();
         }
     }




Comment
Add comment · Show 4
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 reza74vi · Apr 17, 2020 at 06:58 AM 0
Share

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?

avatar image qobion · Apr 17, 2020 at 07:21 AM 0
Share

$$anonymous$$ight be your door colider is too small

avatar image reza74vi qobion · Apr 17, 2020 at 08:44 AM 0
Share

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.

avatar image yummy81 · Apr 19, 2020 at 07:51 AM 0
Share

@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.

3 Replies

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

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

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
0

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")

Comment
Add comment · Show 5 · 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 SpartianTheDev · Apr 17, 2020 at 05:12 PM 0
Share

But this script is joystick script already, did I get it wrong?

avatar image Honorsoft · Apr 17, 2020 at 08:06 PM 0
Share

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.

avatar image reza74vi Honorsoft · Apr 18, 2020 at 05:55 AM 0
Share

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.

avatar image Honorsoft reza74vi · Apr 21, 2020 at 07:11 PM 0
Share

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.

Show more comments
avatar image
0

Answer by SpartianTheDev · Apr 17, 2020 at 02:48 PM

  1. You should check tags that door has "door" tag or player can interact door's collider in
    Project settings >> Physics2D

  2. Try 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

Comment
Add comment · Show 1 · 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 reza74vi · Apr 18, 2020 at 06:15 AM 0
Share

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.

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.

Follow this Question

Answers Answers and Comments

376 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

script does not update inspector? 3 Answers

Boolean Troubles(C#)- Can anyone help? 1 Answer

Distribute terrain in zones 3 Answers

Android C# JoyStick Movement 0 Answers

Please help! After build on Android UI buttons can't enable/disable script but works when using Unity remote. 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