- Home /
The question is answered, right answer was accepted
button not working?
I have tried some possible solutions, none of which have worked. I am converting my first game to a mobile game, a simple game where the only thing you can do is move left and right. I made two buttons, one to move you right, and one to move you left. There are two scripts in the player, one called left and one called right, that look something like this public
public void onPointerUp()
{
IsPointerUp = true;
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
public void FixedUpdate()
{
IsPointerUp = false;
if (IsPointerUp == false)
{
rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.Acceleration);
}
}
(this is the code for the script left) The button has two events, pointer down and pointer up. pointer down triggers fixedupdate and pointer up triggers on pointer up, this is the same on the right button. If i click and hold the button with a debug log script in the if statement, it returns every frame the button is held down, however the cube doesn't move at all. rb is the cube's rigidbody. If i'm just being stupid and need to change my entire code, let me know.
After a break and some thinking, i got it to work, the code now looks something like this
public void OnPointerUp()
{
IsPointerUp = true;
}
public void PointerDown()
{
IsPointerUp = false;
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
public void FixedUpdate()
{
if (IsPointerUp == false)
{
rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, Force$$anonymous$$ode.Acceleration);
}
}
}
Answer by gjf · Mar 30, 2020 at 08:16 PM
onPointerUp()
probably won't get called by Unity. OnPointerUp()
on the other hand....