Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Paratrooper82 · Jun 04, 2018 at 10:18 PM · uiinputbuttonbutton trigger eventskeyboard input

Have a UI Button do what my Input.GetButtonDown("Use") does?

Hey, I have been working on this for days and can't find anything on this topic. I am switching my game from keyboard controls to Mobile UI Button controls. Everything works perfectly, I have a virtual joystick that works on touch, my jump button works and my attack button works. But I can't get my "use" button to work.

My use input has the keyboard input "f" assigned. So when I press "f" next to a switch it drops a box, or if I press "f" on a level door, I enter that level door. That is because I am using Input.GetButtonDown("Use"); in several IF statements throughout my various scripts.

Now I am trying to replace that pressing "f" or Use buttondown with a UI Button. This seems to be a really easy thing, but it is NOT. I know I can attach a trigger event to my button and do the "Pointer Down (Base Event Data)", as that is what I have done with my jump and attack functions. But it doesn't work with my Use input.

Basically I want this UI Button to have the same effect as pressing the letter "f" on the keyboard. Here is a code example of how I use the letter "f" when I am next to a switch.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI; 
 using UnityEngine.EventSystems; 
 
 public class FallingObject : MonoBehaviour {
 
     public GameObject fallingObject;  
 
     private LevelManager theLevelManager; 
 
     public bool waitingForRespawn; 
 
     public SpriteRenderer leverStart; 
     public Sprite  leverMiddle; 
     public Sprite leverRight; 
 
     public AudioSource leverActivation; 
 
 
     //new try for trigger and button
     private bool triggerEntered; 
 
 
 
      
 
     // Use this for initialization
     void Start () {
 
         theLevelManager = FindObjectOfType<LevelManager> ();
 
 
 
     }
     
     // Update is called once per frame
     void Update () {
 
         if (theLevelManager.respawnCoActive) {
             fallingObject.GetComponent<Rigidbody2D> ().isKinematic = true;
             waitingForRespawn = true; 
 
         }
 
         if (waitingForRespawn && !theLevelManager.respawnCoActive) {
             waitingForRespawn = false; 
             leverStart.sprite = leverMiddle; 
         } 
 
 
         if (Input.GetButtonDown("Use") && triggerEntered == true) {
             
                 fallingObject.GetComponent<Rigidbody2D> ().isKinematic = false;
                 leverStart.sprite = leverRight;
                 leverActivation.Play ();
             
             }
         
             
         
     }
 
     public void useButtonClick(){
         Debug.Log ("the button has been clicked"); 
 
     }
 
 
     //this decides if the player is in the zone of the lever
     void OnTriggerStay2D(Collider2D other) {
 
         if(other.tag == "Player"){
             triggerEntered = true; 
         }
     }
 
     void OnTriggerExit2D(Collider2D other){
 
         if (other.tag == "Player") {
             triggerEntered = false;
         }
     }
     }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

211 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

With the new unity input system. How do I setup UI buttons like in the old system. 0 Answers

(using InputSystem) Making Button/Selectable not react to "Submit" action, but without disabling them 1 Answer

Execute code when clicked anywhere except on UI buttons 1 Answer

Button.onClick running too many times 0 Answers

How could I transform keyboard input into touch input? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges