"If" Statement with "bool" Help please

Hello, I have a script for selecting Units. and it works Perfectly. But I want to add some conditions. However, I am obviously doing it wrong. I want to set it, so that if I click on a Unit that is Already Selected, then I would Like it to Deselect that specific unit. Thank you In advance!

  1. My Current Code
  2. what I tried (Does Nothing Functional)

1)-------------------------------------------

public class SnipForForum : MonoBehaviour
{
    void CheckSelectedUnits()                                      
    {

        foreach (Unit myUnits in SelectionManager.unitList)        
        {
            float dist = Vector2.Distance(Input.mousePosition, (Camera.main.WorldToScreenPoint(myUnits.transform.position)));
                                           

             if (dist < 45)
            {
                 myUnits.SetSelector(true);
            }

           
            else  
            {
                myUnits.SetSelector(false);
            }
        }

    }
       
}

2)-----------------------------------------------

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

public class SnipForForum : MonoBehaviour
{
    void CheckSelectedUnits()                                      
    {

        foreach (Unit myUnits in SelectionManager.unitList)        
        {
            float dist = Vector2.Distance(Input.mousePosition, (Camera.main.WorldToScreenPoint(myUnits.transform.position)));
                                           

             if (dist < 45 && myUnits == false)
            {
                 myUnits.SetSelector(true);
            }

           
            else

            if (dist < 45 && myUnits == true)
            {
                myUnits.SetSelector(false);
            }
        }

    }
       
}

myUnit is an instance of Unit, it can’t be true or false. Add a boolean to your Unit class and set/test it.

1 Like

Exactly, myUnity will never be false. You probably want to check for null instead.

1 Like

Sorry, Tottal Noob here, How do I do that? :smile:
Teach me senpi

if (dist < 45 && !myUnits.IsSelected())
{
          myUnits.SetSelector(true);
}

Create IsSelected() to return a boolean storing the state of your unit.

1 Like

Thank you, I will search for some guides on how to do this. :smile:

@LilFire , My “Unit” Script Does Have a bool inside it, cant I just use it? or reference it somehow?

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

public class Unit : MonoBehaviour
{

    public GameObject selectorIndicator;

    void Start()
    {
       
        SetSelector(false);                             
        SelectionManager.unitList.Add(this);        

    }

    public void SetSelector(bool on) 
    {
        selectorIndicator.SetActive(on);
    }
              
}

Your are not using a boolean to store the state of your unit, you are using the GameObject boolean state, which is not a really good solution but i will let you improve it. In this case, here is what you need to do.

public bool IsSelected()
    {
        return selectorIndicator.activeSelf;
    }

This will work, but try using you own boolean.

@LilFire

Thank you

@LilFire , After reading what you said over and over, I think I understand what your saying. Would Have been so great if you were a youtuber and made a guide on this. Instead of Helping me “improve” my method, I am really wondering what you mean by “Your are not using a boolean to store the state of your unit”, what do you mean by storing? like on a list or something? Sorry total noob here :smile:

I am a youtuber myself. I Plan to give full credit to anyone who helps me understand this and I plan to relay this information Later in a video :smile:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Unit : MonoBehaviour
{
    public GameObject selectorIndicator;
    private bool _isSelected;
    void Start()
    {
      
        SetSelector(false);                           
        SelectionManager.unitList.Add(this);       
    }
    public void SetSelector(bool on)
    {
        _isSelected = on;
        selectorIndicator.SetActive(on);
    }

    public bool IsSelected()
    {
       return _isSelected;
     }
            
}

Now you store the state of your unit in _isSelected. Don’t use :

myUnits ==false

Use

myUnits.IsSelected()

@LilFire , Thanks Again, I tried it but it doesnt work, maybe I used it wrong.

  if (dist < 45 )                              
                {
                   if(myUnits.IsSelected())
                    {
                        myUnits.SetSelector(false);
                    }
                                     

                }

However, but before I tried your way. I did something that Semi Worked.


if (dist < 45 && myUnits.enabled)                       
               {
               
                   myUnits.SetSelector(false);
                  
               }

Else                 // ** This doesn't Work because this "Else" statement never gets called.

if (dist < 45 )                       
               {
               
                   myUnits.SetSelector(true);
                  
               }

Thank you for your patience

I am Starting to think, Deselecting Individual Units from a group of selected units is not worth it? lol

OMG I think I have a solution, what if, I Make 2 Lists, one storing units that are Selected, and other that Has Units that Are not Selected. This Also would mean every time I select a unit I have to Add and Remove them from one list to another.

Please tell me what you think?

Computers are like evil genies: they’ll grant your wishes, but you need to get the wording perfect, because they’re going to give you exactly what you asked for (no matter how stupid that was) and not what you meant. Computers have no common sense and need detailed hand-holding instructions for every tiny thing you want them to do.

It looks to me like you are guessing at phrases that mean kinda sorta what you have in mind and mixing them together and hoping something will stick. This is a really bad strategy for computer programming; it’s unlikely to work.

You need to think through all the steps with mathematical rigor and make sure that the words you are saying match exactly with what you want to happen.

if (dist < 45 && myUnits.enabled)
This doesn’t work because “enabled” is just checking whether your script component is turned on.

The computer is never going to figure out that you meant to check whether the unit was selected. It’s just going to check whether the unit was enabled, because that’s what you asked for. You need to ask for exactly what you want or it won’t happen.

if (dist < 45)
{
    if (myUnits.IsSelected())
    {
        myUnits.SetSelector(false);
    }
}

This isn’t going to work because you’re not doing anything in the case where dist < 45 but IsSelected returns false. You’re de-selecting units that are already selected, but you’re not selecting units that were previously unselected.

You need to get the structure of your if/else checks correct or else your code will fail even if you are checking the right conditions.

(And that’s assuming that you correctly implemented IsSelected() in the first place. There’s no way for me to know from your post whether you did or not.)

@Antistone , thank you for reply. I totally agree with you, I am still in my early Learning process so I am making tones of stupid mistakes. Most of what I am doing is just trial and error. tbh every time I search for guides about Lists etc… I get just the basics.

Thanks again, you hit the nail on the head. I am not sure if there is a faster way to learn this stuff, most of what I am learning from guides I already know (or at least I think I do). but yea the whole point of this project for me is to learn programming