Search Unity

"If" Statement with "bool" Help please

Discussion in 'Scripting' started by Mortalanimal, Oct 15, 2019.

  1. Mortalanimal

    Mortalanimal

    Joined:
    Jun 7, 2014
    Posts:
    568
    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)-------------------------------------------


    Code (CSharp):
    1. public class SnipForForum : MonoBehaviour
    2. {
    3.     void CheckSelectedUnits()                                      
    4.     {
    5.  
    6.         foreach (Unit myUnits in SelectionManager.unitList)        
    7.         {
    8.             float dist = Vector2.Distance(Input.mousePosition, (Camera.main.WorldToScreenPoint(myUnits.transform.position)));
    9.                                            
    10.  
    11.              if (dist < 45)
    12.             {
    13.                  myUnits.SetSelector(true);
    14.             }
    15.  
    16.            
    17.             else  
    18.             {
    19.                 myUnits.SetSelector(false);
    20.             }
    21.         }
    22.  
    23.     }
    24.        
    25. }

    2)-----------------------------------------------
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SnipForForum : MonoBehaviour
    6. {
    7.     void CheckSelectedUnits()                                      
    8.     {
    9.  
    10.         foreach (Unit myUnits in SelectionManager.unitList)        
    11.         {
    12.             float dist = Vector2.Distance(Input.mousePosition, (Camera.main.WorldToScreenPoint(myUnits.transform.position)));
    13.                                            
    14.  
    15.              if (dist < 45 && myUnits == false)
    16.             {
    17.                  myUnits.SetSelector(true);
    18.             }
    19.  
    20.            
    21.             else
    22.  
    23.             if (dist < 45 && myUnits == true)
    24.             {
    25.                 myUnits.SetSelector(false);
    26.             }
    27.         }
    28.  
    29.     }
    30.        
    31. }
    32.  
    33.  
     
  2. LilFire

    LilFire

    Joined:
    Jul 11, 2017
    Posts:
    74
    myUnit is an instance of Unit, it can't be true or false. Add a boolean to your Unit class and set/test it.
     
    Mortalanimal likes this.
  3. ProtagonistKun

    ProtagonistKun

    Joined:
    Nov 26, 2015
    Posts:
    352
    Exactly, myUnity will never be false. You probably want to check for null instead.
     
    Mortalanimal likes this.
  4. Mortalanimal

    Mortalanimal

    Joined:
    Jun 7, 2014
    Posts:
    568
    Sorry, Tottal Noob here, How do I do that? :D
    Teach me senpi
     
  5. LilFire

    LilFire

    Joined:
    Jul 11, 2017
    Posts:
    74
    Code (CSharp):
    1. if (dist < 45 && !myUnits.IsSelected())
    2. {
    3.           myUnits.SetSelector(true);
    4. }
    Create IsSelected() to return a boolean storing the state of your unit.
     
    Mortalanimal likes this.
  6. Mortalanimal

    Mortalanimal

    Joined:
    Jun 7, 2014
    Posts:
    568
    Thank you, I will search for some guides on how to do this. :D
     
  7. Mortalanimal

    Mortalanimal

    Joined:
    Jun 7, 2014
    Posts:
    568
    @LilFire , My "Unit" Script Does Have a bool inside it, cant I just use it? or reference it somehow?


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Unit : MonoBehaviour
    6. {
    7.  
    8.     public GameObject selectorIndicator;
    9.  
    10.     void Start()
    11.     {
    12.        
    13.         SetSelector(false);                            
    14.         SelectionManager.unitList.Add(this);        
    15.  
    16.     }
    17.  
    18.     public void SetSelector(bool on)
    19.     {
    20.         selectorIndicator.SetActive(on);
    21.     }
    22.              
    23. }
    24.  
     
  8. LilFire

    LilFire

    Joined:
    Jul 11, 2017
    Posts:
    74
    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.
    Code (CSharp):
    1. public bool IsSelected()
    2.     {
    3.         return selectorIndicator.activeSelf;
    4.     }
    This will work, but try using you own boolean.
     
  9. Mortalanimal

    Mortalanimal

    Joined:
    Jun 7, 2014
    Posts:
    568
    @LilFire

    Thank you
     
  10. Mortalanimal

    Mortalanimal

    Joined:
    Jun 7, 2014
    Posts:
    568
    @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 :D

    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 :D
     
  11. LilFire

    LilFire

    Joined:
    Jul 11, 2017
    Posts:
    74
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class Unit : MonoBehaviour
    5. {
    6.     public GameObject selectorIndicator;
    7.     private bool _isSelected;
    8.     void Start()
    9.     {
    10.      
    11.         SetSelector(false);                          
    12.         SelectionManager.unitList.Add(this);      
    13.     }
    14.     public void SetSelector(bool on)
    15.     {
    16.         _isSelected = on;
    17.         selectorIndicator.SetActive(on);
    18.     }
    19.  
    20.     public bool IsSelected()
    21.     {
    22.        return _isSelected;
    23.      }
    24.            
    25. }
     
  12. LilFire

    LilFire

    Joined:
    Jul 11, 2017
    Posts:
    74
    Now you store the state of your unit in _isSelected. Don't use :
    Code (CSharp):
    1. myUnits ==false
    Use
    Code (CSharp):
    1. myUnits.IsSelected()
     
  13. Mortalanimal

    Mortalanimal

    Joined:
    Jun 7, 2014
    Posts:
    568
    @LilFire , Thanks Again, I tried it but it doesnt work, maybe I used it wrong.

    Code (CSharp):
    1.   if (dist < 45 )                              
    2.                 {
    3.                    if(myUnits.IsSelected())
    4.                     {
    5.                         myUnits.SetSelector(false);
    6.                     }
    7.                                      
    8.  
    9.                 }
    ---------------------------------------------------------------------------

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

    ---------------------------------------------------------------------------

    Code (CSharp):
    1. if (dist < 45 && myUnits.enabled)                      
    2.                {
    3.                
    4.                    myUnits.SetSelector(false);
    5.                  
    6.                }
    7.  
    8. Else                 // ** This doesn't Work because this "Else" statement never gets called.
    9.  
    10. if (dist < 45 )                      
    11.                {
    12.                
    13.                    myUnits.SetSelector(true);
    14.                  
    15.                }
    16.  
    17.  
    18.  

    ---------------------------

    Thank you for your patience
     
  14. Mortalanimal

    Mortalanimal

    Joined:
    Jun 7, 2014
    Posts:
    568
    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?
     
  15. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    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.

    Code (CSharp):
    1. if (dist < 45)
    2. {
    3.     if (myUnits.IsSelected())
    4.     {
    5.         myUnits.SetSelector(false);
    6.     }
    7. }
    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.)
     
  16. Mortalanimal

    Mortalanimal

    Joined:
    Jun 7, 2014
    Posts:
    568
    @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