Search Unity

[Resolved] Movement happening immediately after UI setActive(false)

Discussion in '2D' started by Moofun, Dec 4, 2019.

  1. Moofun

    Moofun

    Joined:
    Jul 18, 2015
    Posts:
    3
    Hello,

    This is my first post here. I will try to keep it to the point. I've searched a lot for the issue I'm having and either cannot find what I'm looking for, or am searching the wrong thing. I think I know what may be going on, but I'm not sure how to solve it at this time.

    I have this very basic ShopScript right now. When the player dies, this shop screen opens. While the shop screen is open, the player cannot move. I have that part working using the isShopActive() method shown below. At this time, the only button on the shop screen that works is the back button to close the shop panel. The button calls deactivateShop() onClick.

    This is actually working, the shop closes and opens as expected. The problem, is that I have movement happening in an Update() on a different script. The movement happens on GetMouseButtonUp. So when I click the back button, the shop closes (as expected) and the player immediately moves towards the mouse click.

    I think what is happening is that the Panel closes and then the Update() runs and sees that the panel isn't active, so the player moves towards the click because the panel isn't actually active.

    I'm trying to figure out how to stop this from happening. I was playing around with yield for a little bit, but that didn't seem to work for me. I saw some suggestions to check if it was on screen, but following my line of thinking here, as soon as it's not active, it shouldn't be on the screen anymore right?

    Code (CSharp):
    1. public class ShopScript : MonoBehaviour
    2. {
    3.     [SerializeField]
    4.     private RectTransform shopPanel;
    5.  
    6.     private int speedUpgrade;
    7.     private int jumpUpgrade;
    8.  
    9.     void Start()
    10.     {
    11.         shopPanel = GetComponent<RectTransform>();
    12.         speedUpgrade = 0;
    13.         jumpUpgrade = 0;
    14.     }
    15.  
    16.     public bool isShopActive()
    17.     {
    18.         return shopPanel.gameObject.activeSelf;
    19.     }
    20.  
    21.     public void activateShop()
    22.     {
    23.         shopPanel.gameObject.SetActive(true);
    24.     }
    25.  
    26.     public void deactivateShop()
    27.     {
    28.         shopPanel.gameObject.SetActive(false);
    29.     }
    30. }
    EDIT
    Thought I posted my update() code. Here it is.

    Code (CSharp):
    1.     private void Update()
    2.     {
    3.  
    4.         if (Input.GetMouseButtonUp(0) && jumpScript.currentJumpBar != 0 && shop.isShopActive() == false)
    5.         {
    6.             rb.AddRelativeForce(getDirection() * calculateMovementSpeedBasedOffCurrentJumpBar());
    7.             jumpScript.ReduceJumpBar();
    8.         }
    9.  
    10.     }
    Any thoughts or advice would be greatly appreciated.
     
    Last edited: Dec 4, 2019
  2. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    164
    Just a shot in the dark, but maybe if you set a conditional on the Player's movement in the update function to only allow movement when isShopActive is equal to false. Maybe something like this:
    Code (CSharp):
    1. void Update()
    2. {
    3. if(isShopActive == false)
    4. {
    5. MovementFunction();
    6. }
    7. }
    Just a thought, ive done something similar in VR and it worked well for me.
     
    MisterSkitz likes this.
  3. Moofun

    Moofun

    Joined:
    Jul 18, 2015
    Posts:
    3
    Woops! I thought I posted my movement code. I'm at work right now, so I can't post the script here quite yet. There is three conditions being checked before deciding whether should movement should be allowed.

    1. The GetMouseButtonUp is one of them.
    2. I have a bar that decides whether you can make a move or not. So it checks if the bar is > 0.
    3. The last is what you've shown here. I check isShopActive() to get true or false back.
     
  4. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    776
    You need to set up an IENumerator to pause at least 0.25f after you click the back button. This delay will keep your click event while IsShopActive is true so the player should not move. Would look similar to this:

    Code (CSharp):
    1. IEnumerator ClickButtonDelay()
    2. {
    3. yield return new WaitForSeconds(0.25f);
    4. isShopActive = false;
    5. }
    Then in your code for your back button:

    Code (CSharp):
    1. StartCoroutine(ClickButtonDelay());
     
    Last edited: Dec 4, 2019
    Cornysam and Moofun like this.
  5. Moofun

    Moofun

    Joined:
    Jul 18, 2015
    Posts:
    3
    That worked perfectly. Thank you a ton, I was trying to use yield like this at one point, but was obviously missing some stuff. Thank you very much.

    I'll add the code that has the changes you recommended. Maybe it will help someone in the future if they run into this issue. Thanks again!

    Code (CSharp):
    1. public class ShopScript : MonoBehaviour
    2. {
    3.     [SerializeField]
    4.     private RectTransform shopPanel;
    5.  
    6.     private int speedUpgrade;
    7.     private int jumpUpgrade;
    8.     private int chainUpgrade;
    9.  
    10.     void Start()
    11.     {
    12.         shopPanel = GetComponent<RectTransform>();
    13.         speedUpgrade = 0;
    14.         jumpUpgrade = 0;
    15.         chainUpgrade = 0;
    16.     }
    17.  
    18.     public bool isShopActive()
    19.     {
    20.         return shopPanel.gameObject.activeSelf;
    21.     }
    22.  
    23.     public void activateShop()
    24.     {
    25.         shopPanel.gameObject.SetActive(true);
    26.     }
    27.  
    28.     public void deactivateShop()
    29.     {
    30.         StartCoroutine(BackButtonDelay());
    31.     }
    32.  
    33.     IEnumerator BackButtonDelay()
    34.     {
    35.         yield return new WaitForSeconds(0.25f);
    36.         shopPanel.gameObject.SetActive(false);
    37.     }
    38. }
     
    MisterSkitz likes this.
  6. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    776
    You are welcome amigo :)
    Glad to help!