Search Unity

Only one bool true at a time

Discussion in 'Scripting' started by petizero, Sep 18, 2020.

  1. petizero

    petizero

    Joined:
    Jan 1, 2019
    Posts:
    47
    Hey, so i wrote a really simple script for debugging and such before i move to more complicated stuff
    here is what it does
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ClickOn : MonoBehaviour
    6. {
    7.     public bool clickedOn;
    8.      void OnMouseDown()
    9.     {
    10.         clickedOn = true;
    11.     }
    12. }
    13.  
    All it does is set that one bool on a cube true if that cube has been clicked on.
    But what im trying to achive is only one cube being able to be true at a time, as this will be used for selecting destinations for movement in the game. So any help with only one being true at a time?
     
  2. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    You could create a static event inside
    ClickedOn
    that passes in a reference to itself.
    The event would be invoked in
    OnMouseDown
    , and each
    ClickedOn
    instance will compare if the reference passed in the event is itself.

    Example:
    Code (CSharp):
    1. public class ClickOn : MonoBehavior {
    2.    private static event Action<ClickOn> OnClicked;
    3.    public bool clickedOn;
    4.  
    5.    void Awake() => OnClicked += CompareClicked;
    6.  
    7.    void OnDestroy() => OnClicked -= CompareClicked;
    8.  
    9.    void OnMouseDown() => OnClicked?.Invoke(this);
    10.  
    11.    //This method is called when any ClickOn object is clicked in the scene.
    12.    //This instance’s clickedOn value will be set to true if the ClickOn object that was clicked is this object.
    13.    void CompareClicked(ClickOn clickedReference) => clickedOn = clickedReference == this;
    14. }
     
    Bunny83 and Dextozz like this.
  3. Dextozz

    Dextozz

    Joined:
    Apr 8, 2018
    Posts:
    493
    In your case, it should be very simple to do something like this via static variables.
    Code (CSharp):
    1. public class ClickOn : MonoBehaviour
    2. {
    3.     public static ClickOn lastClickedOn;
    4.     public bool clickedOn;
    5.    
    6.     void OnMouseDown()
    7.     {
    8.         if(lastClickedOn != null)
    9.         {
    10.             lastClickedOn.clickedOn = false;
    11.         }
    12.         lastClickedOn = this;
    13.        
    14.         clickedOn = true;
    15.     }
    16. }
    So now, every time you click on a cube, it resets the last cube clicked on and says this is the new one. Quite simple.

    Keep in mind: There is probably a better alternative here. Static variables are quite dangerous and I prefer to avoid them as much as possible, so take this code snippet as just an idea of what will work for your scenario. If this is ALL you want to accomplish, this is good enough, but in the future, you will almost certainly need to re-think things.
     
    Bunny83 likes this.