Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Switch Statements Running through all code

Discussion in 'Scripting' started by Lexo18, Jun 16, 2015.

  1. Lexo18

    Lexo18

    Joined:
    May 12, 2013
    Posts:
    34
    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.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MovementInput : MonoBehaviour {
    5.     public enum ButtonType{Up, Down, Left, Right};
    6.     public ButtonType button;
    7.     static bool mover = false;
    8.      public float speed = 20;
    9.      public GameObject player;
    10.      float myX, myY;
    11.     // Use this for initialization
    12.  
    13.     public void reset () {
    14.         mover = false;
    15.     }
    16.    
    17.     // Update is called once per frame
    18.     public void move () {
    19.         mover = true;
    20.     }
    21.     void Update(){
    22.         if(mover){
    23.             switch (button){
    24.             case ButtonType.Down:  
    25.                 myY = -speed;
    26.                 Debug.Log ("down");
    27.                 break;
    28.            
    29.             case ButtonType.Left:
    30.                 myX = -speed;
    31.                 Debug.Log ("left");
    32.                 break;
    33.                
    34.             case ButtonType.Right:
    35.                 myX = speed;
    36.                 Debug.Log ("right");
    37.                 break;
    38.            
    39.         case ButtonType.Up:
    40.                 myY = speed;
    41.                 Debug.Log ("up");
    42.                 break;
    43.             }
    44.         } else {
    45.             myX = 0;
    46.             myY= 0;
    47.         }
    48.         player.transform.Translate(myX * Time.deltaTime, myY * Time.deltaTime, 0);
    49.     }
    50. }
    51.  
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    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.
     
    sarthakz9 likes this.
  3. Lexo18

    Lexo18

    Joined:
    May 12, 2013
    Posts:
    34
    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
     
  4. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,745
    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.
     
  5. Lexo18

    Lexo18

    Joined:
    May 12, 2013
    Posts:
    34
    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.
     
    Last edited: Jun 17, 2015
  6. blizzy

    blizzy

    Joined:
    Apr 27, 2014
    Posts:
    775
    This is literally impossible with the code you posted. Add more debugging output.
     
  7. Korno

    Korno

    Joined:
    Oct 26, 2014
    Posts:
    518
    Do you have multiple copies of the script running at the same time? With different values for ButtonType?
     
  8. Zaladur

    Zaladur

    Joined:
    Oct 20, 2012
    Posts:
    392
    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.
     
  9. blizzy

    blizzy

    Joined:
    Apr 27, 2014
    Posts:
    775
    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.