Search Unity

Buttons click properly but do not respond (output)

Discussion in 'UGUI & TextMesh Pro' started by bitlysub2anf, May 21, 2020.

  1. bitlysub2anf

    bitlysub2anf

    Joined:
    May 8, 2020
    Posts:
    71
    So, I was watching this video on YouTube and exactly followed each steps. Fortunately, everything went well. Until, I was further a platformer game and then tested it. No errors, just that the buttons clicked but did not responed. I came to a point until I created a new project just to check if there was some error with any other scripts causing this to happen. Nope, it still did not work. I again followed that video no result. You can notice in the images that I followed exactly everything. What do I do Anyways, heres the scene: Platformer game, character does not moves with pressing buttons aka scripts not responding at all, platform android

    Note: If you need more info please tell me
    Annotation 2020-05-21 073938.png Annotation 2020-05-21 073938.png Annotation 2020-05-21 073938.png Annotation 2020-05-21 0739389.png
     
  2. Ruchir

    Ruchir

    Joined:
    May 26, 2015
    Posts:
    934
    You need to add an event in the On Click() event you see above the "Axis Touch Button" script
    and then assign the function you wish to call, you can google it or see the unity documentation for this
     
  3. bitlysub2anf

    bitlysub2anf

    Joined:
    May 8, 2020
    Posts:
    71
    Annotation 2020-05-22 062634.png Okay, thanks. I got that all fixed. Now, there is only one thing that I need for a platformer. The most important thing after moving right and left. Aside from all the scrore and, making the camera follow the player and all that stuff: Jump
    So here's how it went. Also, I looked up a tutorial from the following video. Now, only the Jump dosen't work. And this warning appears. Also, the other Jump script called "Jump" is a script from another video in which we touch (anywhere including the right and left buttons) to jump which is not what I want.
    Also, I forgot to tell my expeterise. Anyways, my expertise level is: Noob, can create easter egg apps
    Annotation 2020-05-22 062634.png
     
  4. bitlysub2anf

    bitlysub2anf

    Joined:
    May 8, 2020
    Posts:
    71
    One more thing, I am sory. I souble click. This causes a picture to appear two times (when I forget to click once this happens). Thanks for your time ;)
     
  5. Ruchir

    Ruchir

    Joined:
    May 26, 2015
    Posts:
    934
    Just read what the console message says, that's the problem, your script can't apply jump force because it doesn't have a reference to the Rigidbody2D you have on your yellow character
     
  6. bitlysub2anf

    bitlysub2anf

    Joined:
    May 8, 2020
    Posts:
    71
    So what can I do to fix it? And how?
     
  7. bitlysub2anf

    bitlysub2anf

    Joined:
    May 8, 2020
    Posts:
    71
    Annotation 2020-05-23 225035.png Oh wait, the script you're talking about is another script that is touch anywhere to jump. I just want the player's body to jump when he clicks the specific button made to jump. Take a look at the code

    Code (CSharp):
    1. //This script has been assigned to the player
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityStandardAssets.CrossPlatformInput;
    6.  
    7. public class CatControl : MonoBehaviour {
    8.  
    9.     float dirX;
    10.     public float moveSpeed = 5f;
    11.     Rigidbody2D rb;
    12.     bool facingRight = true;
    13.     Vector3 localScale;
    14.  
    15.     void Start () {
    16.         localScale = transform.localScale;
    17.         rb = GetComponent<Rigidbody2D> ();
    18.     }
    19.  
    20.     void Update () {
    21.         dirX = CrossPlatformInputManager.GetAxis ("Horizontal");
    22.     }
    23.  
    24.     void FixedUpdate()
    25.     {
    26.         rb.velocity = new Vector2 (dirX * moveSpeed, 0);
    27.     }
    28.  
    29.     void LateUpdate()
    30.     {    
    31.         CheckWhereToFace ();
    32.     }
    33.  
    34.     void CheckWhereToFace ()
    35.     {
    36.         if (dirX > 0)
    37.             facingRight = true;
    38.         else if (dirX < 0)
    39.             facingRight = false;
    40.    
    41.         if (((facingRight) && (localScale.x < 0)) || ((!facingRight) && (localScale.x > 0)))
    42.             localScale.x *= -1;
    43.    
    44.         transform.localScale = localScale;
    45.     }
    46. }
    Code (CSharp):
    1. //This script has been assigned to the UI, refer to the above photo for the UI's configuration :)
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class TouchJump : MonoBehaviour
    7. {
    8.  
    9.     private Rigidbody2D rb;
    10.     private float jumpForce = 900f;
    11.  
    12.     private void Start()
    13.     {
    14.         rb = GetComponent<Rigidbody2D>();
    15.     }
    16.  
    17.     private void Update()
    18.     {
    19.          if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
    20.        {
    21.             rb.AddForce(new Vector2(0f, jumpForce), ForceMode2D.Force);
    22.         }
    23.     }
    24. }
    So, how do I make it work?
     
    Last edited: May 23, 2020
  8. bitlysub2anf

    bitlysub2anf

    Joined:
    May 8, 2020
    Posts:
    71
    Forum has been abandoned