Search Unity

Question I'm making a puzzle game mechanic but it's not working properly

Discussion in 'Editor & General Support' started by PanicMedia, Jul 28, 2021.

  1. PanicMedia

    PanicMedia

    Joined:
    Dec 6, 2020
    Posts:
    37
    I'm making a mechanic for my new hacking themed puzzle game where you can type certain phrases and you can then move around the object that is associated with that phrase and when hit the enter key it's supposed to return you to controlling the player character however when you press enter this doesn't happen. Can anyone help? here's the code I used:

    Code (CSharp):
    1.  if(inputField.text == "Blocks")
    2.         {
    3.             cubes.GetComponent<CubeMovement>().enabled = true;
    4.             moveSpeed = 0f;
    5.             if(Input.GetKeyDown(KeyCode.Return))
    6.             {
    7.                cubes.GetComponent<CubeMovement>().enabled = false;
    8.                moveSpeed = 5f;
    9.             }
    10.         }
     
  2. altepTest

    altepTest

    Joined:
    Jul 5, 2012
    Posts:
    1,115
    maybe reset the input field text value in this section

    Code (CSharp):
    1. if(Input.GetKeyDown(KeyCode.Return))
    2.             {
    3.                cubes.GetComponent<CubeMovement>().enabled = false;
    4.                moveSpeed = 5f;
    5.                //reset the input field value
    6.                inputField.text = "";
    7.             }
    if the inputField.text remains the same the entire if will remain true the next frame and it will run again disabling the movement

    this one

    Code (CSharp):
    1. if(inputField.text == "Blocks")