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

The script that calls the button

Discussion in 'Scripting' started by kubunio2005, Mar 24, 2020.

  1. kubunio2005

    kubunio2005

    Joined:
    Mar 23, 2020
    Posts:
    9
    Is there a script that would be able to press the "w" button?
    upload_2020-3-24_13-22-3.png
    I need one that would simulate pressing
     
  2. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    Instead of simulating a press on W, simulate executing what happens when you press W.
    What do you need this for?

    OP wants to simulate a press of the W key, not do something when W is actually being pressed.
     
  3. kubunio2005

    kubunio2005

    Joined:
    Mar 23, 2020
    Posts:
    9
    what should I do with this? I am beginner in programming
     
  4. kubunio2005

    kubunio2005

    Joined:
    Mar 23, 2020
    Posts:
    9
    Or maybe you have other ideas on how to make these buttons work
     
  5. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    Which buttons? The arrow buttons on screen?
    All of this would be a lot easier if you provided more information about what it is you are even trying to do. Until now you asked for how to simulate a press on W. Now you want to use buttons. Since i assume the two questions are linked, for now i'll just assume you want the left and right arrow buttons to do the same thing as W and S, or something along those lines.
    To achieve this, you would write a method MoveForward() and MoveBackward(). You can now trigger MoveForward and MoveBackward when you detect W or S being pressed respectively. You can call the same methods when the buttons are pressed. For this simply add the functions to the OnClick of the UI button.
    Is this what you try to achieve? If not try to describe your problem.
     
  6. kubunio2005

    kubunio2005

    Joined:
    Mar 23, 2020
    Posts:
    9
    I don't care whether the arrows on the screen will simulate pressing or not. I just want them to work
     
  7. kubunio2005

    kubunio2005

    Joined:
    Mar 23, 2020
    Posts:
    9
    I meant it. If you explain the data you need, I will give it to you
     
  8. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    Ok, so since you said you wanted to simulate a press on W, i assume you have some code along those lines already:
    Code (CSharp):
    1. if(Input.GetKey(KeyCode.W)){
    2.     // some code for moving forward
    3. }
    Put the code for the actual movement into its own function instead like so:
    Code (CSharp):
    1. public void MoveForward(){
    2.     // some code for moving forward
    3. }
    Now simply replace the code from the first location with a call to this new function:
    Code (CSharp):
    1. if(Input.GetKey(KeyCode.W)){
    2.     MoveForward();
    3. }
    So far, we are doing the exact same as before.
    However, we can now call this function when the button is pressed as well.

    Your Buttons have a 'Button' script attached. In the inspector you will see an 'OnClick' area. By pressing the +-Button you can add new functions that should be called on click. To be able to select our MoveForward() function there, you need to drag the object with the script containing this method into the corresponding field. Now you can select the MoveForward() function. Pressing the button now executes what is written into the MoveForward() function once.
    This is the default way buttons are used after all.

    However, you most likely want to be able to hold the button pressed and keep running this code every frame. The easiest way to achieve this is to add an 'EventTrigger' component to your button object. We will use this instead of the default OnClick from the button, so if you added your MoveForward function there, you can remove it again.
    In the EventTrigger, using the +-Button you can add an 'UpdateSelected' event. You can assign it a function to run the same way you would do with OnClick. The assigned function should then be run every frame.

    Now you have some code (MoveForward()), which gets called when you press (hold) either the button, or W.
    Do the same for the other direction to go backwards.
     
  9. kubunio2005

    kubunio2005

    Joined:
    Mar 23, 2020
    Posts:
    9
    I do not understand very well. You would be able to do it if I sent you my project
     
  10. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    I'm not doing your work for you, sorry. I did my best to explain what you need to do in beginner friendly language. This may be an english language barrier thing, but if you do not yet feel confident enough in your C# or Unity skills, there are a lot of great tutorials on youtube. I can link you one if you are interrested.
     
  11. kubunio2005

    kubunio2005

    Joined:
    Mar 23, 2020
    Posts:
    9
    I think that the point is that the project was not prepared by me and I can not find it. Thanks for the help