Juan Francisco Patino
Juan Francisco Patino

Reputation: 83

Button Onclick not working with public functions - UNITY3D C#

I'm making an Android game, I assigned public onClick functions to three buttons on the canvas and they're not working on my Android Phone or by clicking them on my computer. Here's what it looks like:

image

And here is my code for the ball, as you can see I'm trying to make the ball move forward with the ForwardSLASHleft function, backwards with the BackwardsSlashRight function, and start with the StartButton function. But none of them are working.

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class ballScript : MonoBehaviour {
    Rigidbody rb;
    public float thrust;
    public Button left;
    public Button right;
    public Button startB;
    // Use this for initialization
    void Start () {
        rb = GetComponent<Rigidbody>();
        gameObject.SetActive(false);

    }

    // Update is called once per frame
    void Update () {
        gameObject.SetActive(true);

    }

    public void StartButton()
    {
        gameObject.SetActive(true);
        left.enabled = true;
        right.enabled = true;
        startB.enabled = false;

    }

    public void forwardSLASHleft()
    {
        rb.AddForce(transform.right * -1 * thrust);
    }

    public void backwardsSLASHright()
    {
        rb.AddForce(transform.right * 1 * thrust);
    }
}

Upvotes: 0

Views: 1056

Answers (1)

Faisal Khalid
Faisal Khalid

Reputation: 650

Drag your script to your Buttons Onclick component and select your function

Also make sure that you have EventSystem GameObject

Upvotes: 1

Related Questions