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?
public class ShopScript : MonoBehaviour
{
[SerializeField]
private RectTransform shopPanel;
private int speedUpgrade;
private int jumpUpgrade;
void Start()
{
shopPanel = GetComponent<RectTransform>();
speedUpgrade = 0;
jumpUpgrade = 0;
}
public bool isShopActive()
{
return shopPanel.gameObject.activeSelf;
}
public void activateShop()
{
shopPanel.gameObject.SetActive(true);
}
public void deactivateShop()
{
shopPanel.gameObject.SetActive(false);
}
}
EDIT
Thought I posted my update() code. Here it is.
private void Update()
{
if (Input.GetMouseButtonUp(0) && jumpScript.currentJumpBar != 0 && shop.isShopActive() == false)
{
rb.AddRelativeForce(getDirection() * calculateMovementSpeedBasedOffCurrentJumpBar());
jumpScript.ReduceJumpBar();
}
}
Any thoughts or advice would be greatly appreciated.