Checking variables

I have a question, in C#, if I try to check if my boolean variable “holding” is true, but it says "the name ‘holding’ does not exist in the current context. This might be a stupid question, but here is the code:

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

public class Pickup_cube : MonoBehaviour {

    // Use this for initialization
    void OnMouseDown () {
        bool holding = true;
    }
   
    // Update is called once per frame
    void OnMouseUp () {
        bool holding = false;
    }
    void Update () {
        if (holding == true) {

        }
    }
}

Declare the variable at the top of your script:

public class Pickup_cube : MonoBehaviour
{

public bool holding;

void OnMouseDown()
{
    holding = true;
}

void OnMouseUp()
{
    holding = false;
}

void Update()
{
    if(holding)
    {
  
    }
}
}
1 Like

Thank you!!