Adding Functions to GUI Buttons?

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

public class OnCollision : MonoBehaviour
{
public Transform other;
public GUIStyle style;

void OnGUI()
{
	if (other)
	{
		float dist = Vector3.Distance(other.position, transform.position);
		if(dist < 15 )
		{
			GUI.Button(new Rect(600, 100, 100, 100), "NO");
			GUI.Button(new Rect(800, 100, 100, 100),"YES");
			GUI.TextArea(new Rect (600, 250, 300, 100), "Do You Want To Open This Crate?", style);
		}
	}
}

}

I have this code that makes 2 GUI Buttons and 1 GUI Text appear on collision. I have two buttons called “YES” and “NO”. I also have a crate in the game. When I click the no button, I want the crate to disappear. When I click the yes button, I want the crate to open with a certain animation. How can I implement it into this code?

This is the legacy UI which you should avoid for in-game UI.

https://docs.unity3d.com/ScriptReference/UI.Button.html

For your answer, GUI.Button returns true if pressed so you can set it in an if- statement:

if(GUI.Button(rect, text))
{
       MethodToBeCalled();
}