Switch Statements Running through all code

In this script, I am aiming to get an object to move in 1 of 4 directions based on given input from the user. This is controlled by a switch statement which checks which respective button is being pressed. The recurring problem is that when the switch statement is called, all 4 parts(buttons/directions/input) of the switch statement run simultaneously. This makes the object never move because the code is trying to make it go up, down, left, and right all at the same time. I pin-pointed the problem to switch statement because when 3 of the buttons(cases) are commented it out, it moves in the desired direction that was left un-commented.

using UnityEngine;
using System.Collections;

public class MovementInput : MonoBehaviour {
    public enum ButtonType{Up, Down, Left, Right};
    public ButtonType button;
    static bool mover = false;
     public float speed = 20;
     public GameObject player;
     float myX, myY;
    // Use this for initialization

    public void reset () {
        mover = false;
    }
   
    // Update is called once per frame
    public void move () {
        mover = true;
    }
    void Update(){
        if(mover){
            switch (button){
            case ButtonType.Down:   
                myY = -speed;
                Debug.Log ("down");
                break;
           
            case ButtonType.Left:
                myX = -speed;
                Debug.Log ("left");
                break;
               
            case ButtonType.Right:
                myX = speed;
                Debug.Log ("right");
                break;
           
        case ButtonType.Up:
                myY = speed;
                Debug.Log ("up");
                break;
            }
        } else {
            myX = 0;
            myY= 0;
        }
        player.transform.Translate(myX * Time.deltaTime, myY * Time.deltaTime, 0);
    }
}

That can’t possibly be true because if all four cases were running, your object would move up and to the right as they’re the last two cases.

Where / when do you call move() to set mover to true? Where / when do you assign button?

Your reset needs a capital R, also.

1 Like

Its placed on UI elements with Event Triggers in an emulated D-pad fashion.
Move is called with Pointer Down and reset is Pointer Up

Have you confirmed that Button is a valid value? One thing he asked that you didn’t answer is, where do you assign anything to button?

Have you confirmed that “mover” is being set to true?

As @GroZZleR said, I think you’ve misdiagnosed the problem. if every case of the switch were being run, then only the last ones would ultimately have any effect, since you’re assigning things there and not adding things. You’d be always moving up and to the right - not standing still.

Button is being set in the inspector since its a public enumeration and mover is being set as true, as stated before when using Debug.Log all of the cases in the switch statement are being ran at once and therefore canceling all movement from happening.

This is literally impossible with the code you posted. Add more debugging output.

Do you have multiple copies of the script running at the same time? With different values for ButtonType?

If all switch statements ran at once, the movement would not be cancelled out. Make a new scene, put just the scripts required for movement onto a cube that you set as the player. Also post the code that sets button. Maybe you are cycling that somehow each frame - but in a given frame, movement is only going to attempt one direction.

Also - more Debugs. Debug the value of button just before the switch statement. This will prove that only one case is being run at a time.

It also helps to debug print the current frame number. Or step through each single frame with the play/pause buttons at the top of the editor window.