Search Unity

UI button is not working

Discussion in 'Scripting' started by bitlysub2anf, May 26, 2020.

  1. bitlysub2anf

    bitlysub2anf

    Joined:
    May 8, 2020
    Posts:
    71
    I cannot seem to make my jump button work, my right and left button do work but for some reason jump does not work. I followed steps from this video exactly. Any ideas?

    And yes, I am developing my project for the Android Platform
     
  2. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    No, without seeing what you have done or your code, we have no idea what is wrong.
     
  3. bitlysub2anf

    bitlysub2anf

    Joined:
    May 8, 2020
    Posts:
    71
    I copied everything from the video but, I don't mind showing my progress so far. Take a look:
    Annotation 2020-05-26 164405.png
    This is the jump button in the inspector with an event trigger and a button handler script. Here is the script:
    Code (CSharp):
    1. susing System;
    2. using UnityEngine;
    3.  
    4. namespace UnityStandardAssets.CrossPlatformInput
    5. {
    6.     public class ButtonHandler : MonoBehaviour
    7.     {
    8.  
    9.         public string Name;
    10.  
    11.         void OnEnable()
    12.         {
    13.  
    14.         }
    15.  
    16.         public void SetDownState()
    17.         {
    18.             CrossPlatformInputManager.SetButtonDown(Name);
    19.         }
    20.  
    21.  
    22.         public void SetUpState()
    23.         {
    24.             CrossPlatformInputManager.SetButtonUp(Name);
    25.         }
    26.  
    27.  
    28.         public void SetAxisPositiveState()
    29.         {
    30.             CrossPlatformInputManager.SetAxisPositive(Name);
    31.         }
    32.  
    33.  
    34.         public void SetAxisNeutralState()
    35.         {
    36.             CrossPlatformInputManager.SetAxisZero(Name);
    37.         }
    38.  
    39.  
    40.         public void SetAxisNegativeState()
    41.         {
    42.             CrossPlatformInputManager.SetAxisNegative(Name);
    43.         }
    44.  
    45.         public void Update()
    46.         {
    47.  
    48.         }
    49.     }
    50. }
    This is a fresh piece from the Standard Assets.

    This is everything related to the Jump Button and I see no errors in the Console. Please do not abandon me.
     
  4. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    Stick some Debug.Log in your set state down and up methods to check they are working and then look at whatever method you have running when that button is down. Also check you have a Jump button defined in the project settings.
     
  5. bitlysub2anf

    bitlysub2anf

    Joined:
    May 8, 2020
    Posts:
    71
    Okay, that means I need to attch my script to an empty game object. But what do I write in it? (Sorry if I am giving you a headache, I am new and all I can do is read scripts and learn their meanings and I never had to write a script of this type). (In other words: Nani?)
     
    Last edited: May 27, 2020
  6. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    The script is already on the object you pictured. Just put some logs in the up and down methods. Something like Debug.Log(Name + " button down"); to check that script is working. If it is, check the method you call when that button is in the down state.
     
  7. bitlysub2anf

    bitlysub2anf

    Joined:
    May 8, 2020
    Posts:
    71
    This is what I came up with
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class DebuggerComponent : MonoBehaviour
    5. {
    6.  
    7.    void Bugdetect()
    8.    {
    9. //I think I need to add if inputgettouch or something, i am not sure what to type and if that does go there, do i add "then" below
    10.        Debug.Log(Result + " button down");
    11.    }
    12. }
    Is it correct, and what do I add it to?
     
    Last edited: May 29, 2020
  8. bitlysub2anf

    bitlysub2anf

    Joined:
    May 8, 2020
    Posts:
    71
    Okay, nevermind that. I used this script:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class DebuggerComponent : MonoBehaviour
    5. {
    6.     void Update()
    7.     {
    8.         if (Input.GetMouseButton(0))
    9.         {
    10.             Debug.Log("Pressed left click.");
    11.         }
    12. }
    I use Unity Remote (5) with my Android Games, and when clicked on my phone, I did get the debug that: "Pressed left click." from the Debug.Log. So, when I press the jump button I get the message, but I noticed something weird, (even though I attached it to the jump button) it applied to anyposition I click on the screen. Okay, so what do I do now?
    Annotation 2020-05-29 191415.png
     
  9. bitlysub2anf

    bitlysub2anf

    Joined:
    May 8, 2020
    Posts:
    71
    Hello, anyone?
    Is anyone even going to help me
     
  10. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    As I said, put some Debug.Log in your methods which that button is calling. Just logging when the mouse is clicked is only going to tell you that the mouse is clicked, it isn't telling you if your button logic is working.
     
  11. bitlysub2anf

    bitlysub2anf

    Joined:
    May 8, 2020
    Posts:
    71
    Funny story, I found some problem in the event trigger and fixed it. Now, the very last thing that I need to close this case is of course, the problem that comes with every jump script, infinite jumps. Now, I am just giving you an update. I will find some video or something (CodeMonkey has a video on his channel). But, just in case if something goes wrong (again), please provide me assistance with it. Annotation 2020-05-30 154804.png
    Update: Every single video and forum (that I opened) are for PC. So, any idea what to do?
     
    Last edited: May 30, 2020
  12. bitlysub2anf

    bitlysub2anf

    Joined:
    May 8, 2020
    Posts:
    71
    CASE CLOSED
    NOTE: THIS THREAD IS FOR JUMP BUTTON MOVEMENT
    IT MAY NOT HELP OUT, BUT IT IS WORTH A SHOT