toggle boolean by clicking a gameobject

Hi guys, beginner here ,
I feel super bad to ask something that simple…
I’d like to make a simple toggle function.

I have a game object, with a boolean variable.
When I click on it , i’d like it to be “true”
When I release the clic, I’d like it to stay “true”

When I reclick, i’d like it to be “false”
When I release the clic, I’d like it to stay “false”

I tried all sort of nonsense “if” condition, but nothing worked.
I thoughtn there was a “!=” condition, but I couldn’t get it working.

I’m sure something simple exist.
Thank you

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

public class allowChangeColor : MonoBehaviour
{
    public static bool allowedToColor = false;
    //public bool isclicked;


    void Start () {}

    void Update () {}

    void OnMouseUp()
    {
        allowedToColor = false;
        //Debug.Log(allowedToColor);
    }

    void OnMouseDown()
    {       
        allowedToColor = true;
        //Debug.Log(allowedToColor);
    }

}

Try this:

public class allowChangeColor : MonoBehaviour
{
    public static bool allowedToColor = false;

    private void OnMouseDown()
    {
        if (!allowedToColor)
        {
            allowedToColor = true;
        }
        else
        {
            allowedToColor = false;
        }
    }
}

You could use this: Unity - Scripting API: MonoBehaviour.OnMouseUpAsButton()

just put: allowedToColor = !allowedToColor; // reversing whatever the current bool state is.

OnMouseUpAsButton means the down and up clicks have to both occur while the mouse is over the same object. :slight_smile:

You are better off using the EventSystem and IPointerClickHandler. That way it will play nice with the UI system.

OnMouseDown is fairly redundant now, and should only be used in legacy projects.

It’s good you mentioned that. I was going to say so (or at least add it to my post).
That is the better approach…

The Vertex Answer was what I was after, Thank you a lot !
I’m going to try to gather infos on EventSystem and IPointerClickHandler, but if you have an example somewhere I’m all for it !
Ah wasn’t aware of OnMouseUpAsButton !
But last time I tried allowedToColor = !allowedToColor; I Monodevelop sent an error…

Thank’s a lot for your help, great community :slight_smile:

There are a bunch of videos on my YouTube channel. Link in my signature. The most useful example is probably the one on blocking clicks.

You’re welcome. Take it easy :slight_smile:

Kiwasi, I love the simplicity of your code (the OnMouseUp part) !
Still trying to figure out the event part :slight_smile:

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

public class mouseResponse : MonoBehaviour {
    bool isRed;            //declare var bool       
    Color initColor;    //declare var color

    void Start()
    {
        initColor = GetComponent<Renderer>().material.GetColor("_Color"); //set la valeur de initColor comme etant celle de la couleur du materiau de base
    }

    public void OnMouseUp(){
        isRed = !isRed;
        if (isRed) {
            GetComponent<Renderer> ().material.color = Color.red;       
        }
        else
        {
            GetComponent<Renderer> ().material.color = initColor;       
        }
    }
}

I get the “overal” idea of the IPointerClickHandler (PointerEventData eventData) thank’s to your tutotial :slight_smile:
Is it right to think it filters the clicks of the mouse considering the UI as a mask (above the 3d objects), or is it something else ?

The official doc doesn’t help much Redirecting to latest version of com.unity.ugui

IPointerClickHandler does the same thing as OnMouseUpAsButton. It just works sensibly with the UI system.