How to turn on and turn off function with button.

Hello guys :slight_smile: pretty easy question,but not so easy to me :smiley: So i have made Cube “sticky” i mean it sticks to every collision that it touches.Well i want to make this a bit different,i want it only stick when i press “f” button and when it is stuck on collision,i walk a bit closer to that cube and press again “f” button then cube is no longer sticky.I hope you understand me,please if you have any questions just ask me,and please help me :o

Here is the script:

using UnityEngine;
using System.Collections;

public class StickyObject : MonoBehaviour {
    void OnCollisionEnter(Collision c) {
        var joint = gameObject.AddComponent<FixedJoint>();
        joint.connectedBody = c.rigidbody;

and javascript too:

function OnCollisionEnter(c : Collision) {
    var joint = gameObject.AddComponent(FixedJoint);
    joint.connectedBody = c.rigidbody;

Use a boolean variable, and set it to true/false when you press the button. Then use this variable to control your function with an if statement.