How to get a GUI button to run a script

Hi,

I have a GUI button, which as the onclick function set to run the Pushback function, but whwn the button is pressed, nothing happens.

Here is the code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Pushbackbutton2 : MonoBehaviour {
    Vector3 tempPos;
    Vector3 tempPosY;
    public GameObject Aircraft;
    
    public void Pushback()
    {
        
        
            InvokeRepeating("Rotate", 0f, 0.25f);
           tempPos = Aircraft.transform.position;
           tempPos.x -= 0.0003f;
           Aircraft.transform.position = tempPos;
           tempPosY = Aircraft.transform.position;
          tempPosY.y -= 0.0001f;
           Aircraft.transform.position = tempPosY;
            Debug.Log("working");
        
        }

    

   void Rotate()
    {
       transform.Rotate(Vector3.back);
    }
}

Does anyone have any ideas?

Thanks

The code is working fine, but you just cant see the difference, because the movement speed is way too slow. Also the rotating function is not even rotating…
I fixed some bugs and improved your code:

    //Variables
    public GameObject Aircraft;

    private bool canMoveaircraft;

    private float Timer;

    public void Pushback()
    {
        //Start rotating aircraft
        InvokeRepeating("Rotate", 0f, 0.01f);
    }

    void Update()
    {
        Debug.Log(Aircraft.transform.rotation.z);

        //Increase timer
        Timer += Time.deltaTime;

        //If the aircraft is turned then:
        if (Aircraft.transform.rotation.z <= -0.8f)
        {
            //Stop rotating aircraft
            CancelInvoke("Rotate");
            canMoveaircraft = true;
        }

        //If boolean canMoveaircraft is enabled:
        if (canMoveaircraft)
        {
            //Set timer to 0
            Timer = 0;

            //If timer is smaller than 5:
            if (Timer < 1f)
            {
                //Move aircraft
                Aircraft.transform.Translate(new Vector3(0, 0.1f, 0));
            }
        }
    }


    void Rotate()
    {
        //Rotate aircraft
        Aircraft.transform.Rotate(Vector3.forward, Time.deltaTime * 20f, Space.Self);
    }

As we don’t see the other script, we cant be sure that its typed correct. What I try if something is not working is to go back to the most simplest form you can think of to make this work. I would try something like this:

public class ButtonToClick : MonoBehaviour{
    
    void OnGUI()
    {
        if(GUI.Button(new Rect(10,10,100,100), "Click this"))){
            GameObject.Find("PushBackButton").GetComponent<PushBackButton>().PushBack();
        }
    }
}
public class PushBackButton : Monobehaviour{

    public void PushBack(){
       Debug.Log("PushBack!");
    }

}

When you are absolutely sure that the communication between scripts is correct, continue adding code.

Did you ensure that the Button’s Image-Component has its “Raycast Target”-Flag toggled to true?

See: Raycast target on UI elements - Questions & Answers - Unity Discussions

If not, that would be a possible reason for the click event not executing.

Seems right. Make sure you GameObject the behavior is attached to is ACTIVE in the inspector when you press play. If you are pulling this from a prefab, you might have issues considering it might not be in the scene.

Fixed using code:

/*==============================================================================

==============================================================================*/

using UnityEngine;
using System.Collections;

public class MoveBackwards : MonoBehaviour 
{
    #region Inspector

    public AudioSource PushbackAudio;

    public float maxRotationDegrees = 10f;
    public float rotationSpeed = 2f;

    public float maxDistance = 5f;
    public float moveSpeed = 2f;

    #endregion //Inspector

    private float traveledDistance;
    private float rotatedAmount;
    private bool isMoving;

    #region Unity Engine & Events

    private void Update()
    {

        AudioSource audio = GetComponent<AudioSource>();
        
        if(isMoving)
        {
            if(traveledDistance < maxDistance)
            {
                Vector3 moveDirection = -transform.up;
                transform.position += moveDirection * moveSpeed * Time.deltaTime;
                traveledDistance += Mathf.Abs(moveSpeed * Time.deltaTime);
            }
            if(rotatedAmount < maxRotationDegrees)
            {
                transform.Rotate(0, 0, rotationSpeed * Time.deltaTime);
                rotatedAmount += Mathf.Abs(rotationSpeed * Time.deltaTime);
            }
        }
    }

    #endregion //Unity Engine & Events

    public void Move()
    {
        PushbackAudio.Play();
        isMoving = true;
    }

}