Q
Q
qsecretp2018-10-17 18:26:09
2D
qsecretp, 2018-10-17 18:26:09

ui button as a button on the keyboard. What to do?

unity. csharp. Wrote a simple script under the clave:

using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;

public class Door : MonoBehaviour {


  public KeyCode aktion = KeyCode.E;
  public int level;
  public bool InTrigger = true;
  public GameObject door;

  public void Start ()
  {
  }

  public void Update ()
  {
    
  }
  public void pressA ()
  {
    Input.GetKey (aktion);
    print ("eee");
  }

     void OnTriggerEnter2D(Collider2D other)
  {
    if (other.gameObject.tag == "Player") {
      InTrigger = true;
    }
  }

  void OnTriggerExit2D(Collider2D other)
  {
    if (other.gameObject.tag == "Player") {
      InTrigger = !InTrigger;
    }
  }

  public void FixedUpdate ()
  {
    if (Input.GetKey (aktion) && InTrigger)  // действие, если на триггере и нажата клавиша
    {
      SceneManager.LoadScene (level); // переход
    }
  }
}

Works as it should. But how to hang an action (KeyCode.E) on a UI Button (for android)?
I'm still green in this matter, so I ask for help.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
GavriKos, 2018-10-17
@GavriKos

UI Button has Events. It is possible through the inspector, it is possible with code ( https://docs.unity3d.com/ScriptReference/UI.Button...

D
Denis Gaydak, 2018-10-18
@MrMureno

I'm a little surprised how people can not find / skip) this important tutorial)
https://unity3d.com/en/learn/tutorials/topics/user...
and the code option is excellent as GavriKos tells you.
code in practice is sometimes even better if you do not have a simple menu that is easy to remember anyway.
it will be easier to find links and where is it called and from where)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question