Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

This has confused me. My IF statement is not working?

Discussion in 'Scripting' started by GeekStories, Oct 16, 2015.

  1. GeekStories

    GeekStories

    Joined:
    Jan 22, 2015
    Posts:
    74
    This has been solved :)


    So this is the statement:

    Code (CSharp):
    1.  
    2. if(shouldSelectPlanet)
    3. {
    4.      //other code that works without this if statement
    5. }
    6.  
    and my problem is that it doesn't want to work.
    When I run the game with out this if statement it works. (not how its suppose to but it does)
    but when I run the game with this if statement it doesn't work at all.

    I have tried it like this:

    Code (CSharp):
    1.  
    2. if(shouldSelectPlanet == true)
    3. {
    4.   //other code that works without this if statement
    5. }
    6.  
    but this still does not work.

    in a bit of script further down that enabled shouldSelectPlanet, underneath that line I have a print statement to check if it IS being changed to TRUE. And it is. Although, it still does not work. I HAVE checked over every possible script for any change it makes and theres nothing. I am really confused and any help would be appreciated thanks!
     
    Last edited: Oct 16, 2015
  2. ofltunisia

    ofltunisia

    Joined:
    Sep 25, 2014
    Posts:
    16
    did you try to initialize this variable when declaring it ??? giving it a value True/false ?
     
  3. GeekStories

    GeekStories

    Joined:
    Jan 22, 2015
    Posts:
    74
    Yep, I have given it a true/false value when declaring it.
     
  4. ofltunisia

    ofltunisia

    Joined:
    Sep 25, 2014
    Posts:
    16
    Maybe Check if their is a code before the If statement blocking the if before even executing it ?
     
  5. GeekStories

    GeekStories

    Joined:
    Jan 22, 2015
    Posts:
    74
    Hmmm, the if statement is inside the Update() function. Also the only code that could possibly cancel it out is inside the if statement at the part that would mean it works. So its really confusing as to why it isn't working :confused:
     
  6. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    The easiest way to get help is to show the whole code, because there might be other things affecting it.
     
    LeftyRighty likes this.
  7. ofltunisia

    ofltunisia

    Joined:
    Sep 25, 2014
    Posts:
    16
    can you post you update() code?
     
  8. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    how about a print statement just before the if so you can see what it is at the point it's "going wrong"...
     
  9. GeekStories

    GeekStories

    Joined:
    Jan 22, 2015
    Posts:
    74
    I shall try that. But I believe it would just print TRUE 60+ times a second

    edit: this is new. Its actually printing false! Now I shall spend the next few hours trying to figure out why :D thank you!
     
  10. GeekStories

    GeekStories

    Joined:
    Jan 22, 2015
    Posts:
    74
    Here is the if statement and its guts WITH the function thats called when a button is clicked

    Code (CSharp):
    1.  
    2. void Update()
    3. {
    4.         print(shouldSelectPlanet);
    5.         if (shouldSelectPlanet) //Is always false for some odd reason
    6.         {
    7.             //Here down WORKS if the above if statement is commented out or deleted
    8.             if (Input.GetMouseButtonDown(0)) //Click the left mouse button
    9.             {
    10.                 Ray ray = Camera.main.GetComponent<Camera>().ScreenPointToRay(Input.mousePosition);
    11.                 RaycastHit hit;
    12.                 if (Physics.Raycast(ray, out hit, Mathf.Infinity))
    13.                 {
    14.                     if (check == 0)
    15.                     {
    16.                         switch (hit.transform.tag)
    17.                         {
    18.                             case "Player":
    19.                                 GameManager.news = "You can not destroy your own planet!";
    20.                                 break;
    21.                             case "Stray":
    22.                                 selectedPlanet = hit.transform.gameObject;
    23.                                 GameManager.news = "Click this planet again to verify you wish to destroy it!";
    24.                                 check++;
    25.                                 break;
    26.                             case "Enemy":
    27.                                 selectedPlanet = hit.transform.gameObject;
    28.                                 GameManager.news = "Click this planet again to verify you wish to destroy it!";
    29.                                 check++;
    30.                                 break;
    31.                             default:
    32.                                 GameManager.news = "Please select a planet to destroy!";
    33.                                 break;
    34.                         }
    35.                     }
    36.                     else if (check == 1 && hit.transform == selectedPlanet.transform)
    37.                     {
    38.                         LaunchRocketAtPlanet();
    39.                         check = 0;
    40.                     }
    41.                     else
    42.                     {
    43.                         check = 0;
    44.                         GameManager.news = "Please select a planet to destroy!";
    45.                     }
    46.                 }
    47.             }
    48.         }
    49. }
    50.  
    51.  
    52. //Function Called When a UGUI button is clicked
    53. public void StartProcess()
    54.     {
    55.         if (PlayerUnits.missiles >= 1)
    56.         {
    57.             if (!GameManager.isBuilding && !GameManager.isUpgrading && !GameManager.isCreatingUnits && !GameManager.isResearching)
    58.             {
    59.                 shouldSelectPlanet = true;
    60.                 GameManager.news = "Please select a planet to destroy!";
    61.                 //Stop ALL processes
    62.                 GameManager.isLaunchingMissile = true;
    63.             }
    64.         }
    65.         else
    66.         {
    67.             CreateMissile();
    68.         }
    69.     }
     
  11. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Or you paste your code here and get help :)
    I know why I wrote that you should post the complete code. It is not that unlikely that you have made your boolean a public field that can be modified in the inspector. You may initialize it in the script to a certain value when you declare it, but that doesn't count when you have it in the inspector.
     
  12. GeekStories

    GeekStories

    Joined:
    Jan 22, 2015
    Posts:
    74
    Haha yep, that has been done just before :)
    the bool for the shouldSelectPlanet is written as
    Code (CSharp):
    1.  
    2. bool shouldSelectPlanet = false;
    3.  
    as nothing but that script needs access to it.
     
  13. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    If you want actual help, don't discuss, post the whole script...
     
  14. GeekStories

    GeekStories

    Joined:
    Jan 22, 2015
    Posts:
    74
    This is the entire script here.
    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4.  
    5. public class LaunchRocket : MonoBehaviour {
    6.  
    7.     public float timeToSelectedPlanet;
    8.     public GameObject selectedPlanet;
    9.     public Text missileText;
    10.     int check = 0;
    11.     bool shouldSelectPlanet = false;
    12.  
    13.     void Start()
    14.     {
    15.         missileText = GameObject.Find("LaunchRocketsText").GetComponent<Text>();
    16.     }
    17.  
    18.     void Update()
    19.     {
    20.         if (PlayerUnits.missiles >= 1)
    21.         {
    22.             missileText.text = "Launch Rocket - " + PlayerUnits.missiles.ToString();
    23.             missileText.GetComponentInParent<Button>().interactable = true;
    24.         }
    25.         else
    26.         {
    27.             if (MissileFactory.canCreateRockets)
    28.             {
    29.                 missileText.GetComponentInParent<Button>().interactable = true;
    30.                 missileText.GetComponent<Text>().text = "Create a rocket";
    31.             }
    32.             else
    33.             {
    34.                 missileText.GetComponentInParent<Button>().interactable = false;
    35.                 missileText.GetComponent<Text>().text = "Create a missile launch bay!";
    36.             }
    37.         }
    38.  
    39.         print(shouldSelectPlanet);
    40.         if (shouldSelectPlanet)
    41.         {
    42.             if (Input.GetMouseButtonDown(0))
    43.             {
    44.                 Ray ray = Camera.main.GetComponent<Camera>().ScreenPointToRay(Input.mousePosition);
    45.                 RaycastHit hit;
    46.                 if (Physics.Raycast(ray, out hit, Mathf.Infinity))
    47.                 {
    48.                     if (check == 0)
    49.                     {
    50.                         switch (hit.transform.tag)
    51.                         {
    52.                             case "Player":
    53.                                 GameManager.news = "You can not destroy your own planet!";
    54.                                 break;
    55.                             case "Stray":
    56.                                 selectedPlanet = hit.transform.gameObject;
    57.                                 GameManager.news = "Click this planet again to verify you wish to destroy it!";
    58.                                 check++;
    59.                                 break;
    60.                             case "Enemy":
    61.                                 selectedPlanet = hit.transform.gameObject;
    62.                                 GameManager.news = "Click this planet again to verify you wish to destroy it!";
    63.                                 check++;
    64.                                 break;
    65.                             default:
    66.                                 GameManager.news = "Please select a planet to destroy!";
    67.                                 break;
    68.                         }
    69.                     }
    70.                     else if (check == 1 && hit.transform == selectedPlanet.transform)
    71.                     {
    72.                         LaunchRocketAtPlanet();
    73.                         check = 0;
    74.                     }
    75.                     else
    76.                     {
    77.                         check = 0;
    78.                         GameManager.news = "Please select a planet to destroy!";
    79.                     }
    80.                 }
    81.             }
    82.         }
    83.     }
    84.  
    85.     public void LaunchRocketAtPlanet()
    86.     {
    87.         timeToSelectedPlanet = Vector3.Distance(GameObject.Find("PlayersPlanet").transform.position, selectedPlanet.transform.position);
    88.         timeToSelectedPlanet = Mathf.RoundToInt(timeToSelectedPlanet);
    89.         GameManager.news = "In " + timeToSelectedPlanet.ToString() + " you will destroy the planet " + selectedPlanet.GetComponent<PlanetController>().planetName + "!";
    90.         GameObject missile = Instantiate(Resources.Load("Missile"), transform.position, Quaternion.identity) as GameObject;
    91.         missile.GetComponent<Missile>().targetPlanet = selectedPlanet;
    92.         PlayerUnits.missiles -= 1;
    93.         GameManager.isLaunchingMissile = false;
    94.         shouldSelectPlanet = false;
    95.  
    96.         selectedPlanet.gameObject.tag = "TARGET";
    97.     }
    98.  
    99.     public void StarProcess()
    100.     {
    101.         if (PlayerUnits.missiles >= 1)
    102.         {
    103.             if (!GameManager.isBuilding && !GameManager.isUpgrading && !GameManager.isCreatingUnits && !GameManager.isResearching)
    104.             {
    105.                 shouldSelectPlanet = true;
    106.                 GameManager.news = "Please select a planet to destroy!";
    107.                 //Stop ALL processes
    108.                 GameManager.isLaunchingMissile = true;
    109.             }
    110.         }
    111.         else
    112.         {
    113.             CreateMissile();
    114.         }
    115.     }
    116.  
    117.     void CreateMissile()
    118.     {
    119.         if (GameObject.Find("PlayersPlanet").GetComponent<PlanetController>().metal >= 1500 &&
    120.             GameObject.Find("PlayersPlanet").GetComponent<PlanetController>().water >= 800 &&
    121.             GameObject.Find("PlayersPlanet").GetComponent<PlanetController>().lightPoints >= 10)
    122.         {
    123.             PlayerUnits.missiles++;
    124.  
    125.             GameObject.Find("PlayersPlanet").GetComponent<PlanetController>().lightPoints -= 10;
    126.             GameObject.Find("PlayersPlanet").GetComponent<PlanetController>().water -= 800;
    127.             GameObject.Find("PlayersPlanet").GetComponent<PlanetController>().metal -= 1500;
    128.         }
    129.     }
    130. }
    131.  
     
  15. UbiquitousStudio

    UbiquitousStudio

    Joined:
    Oct 11, 2015
    Posts:
    24
    Your issue is that your using a bool so it needs to be true or false or it's nothing and won't read. So in your case if has to be if(shouldSelectPlanet == false)
    or (ifShouldSelectPlanet == true)

    This won't give you errors cause it's not wrong it's just not doing anything.
    Hope that helps./
     
  16. GeekStories

    GeekStories

    Joined:
    Jan 22, 2015
    Posts:
    74
    I have tried this and still nothing.
    and I use bools like this in if statements in other scripts perfectly fine too ;)
     
  17. UbiquitousStudio

    UbiquitousStudio

    Joined:
    Oct 11, 2015
    Posts:
    24
    Try switching around the code to have the input first. Then run the if(shouldSelctPlanet == true or false)
    I know I made a building system with a similar issue and it was just switching around the if statements,
    If that doesn't work then try if input.getkeydown && shouldselectplanet == true.
     
  18. GeekStories

    GeekStories

    Joined:
    Jan 22, 2015
    Posts:
    74
    Hmmm. Good idea, I shall try this!

    edit: Combining the two statements for the input and the bool seems to make it loop once but only once.
     
  19. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    Code (CSharp):
    1.         if (shouldSelectPlanet)
    2.         {
    3.             Debug.Log("I'm here 1");
    4.             if (Input.GetMouseButtonDown(0))
    5.             {
    6.                 Debug.Log("I'm here 2");
    7.                 Ray ray = Camera.main.GetComponent<Camera>().ScreenPointToRay(Input.mousePosition);
    8.                 RaycastHit hit;
    9.                 if (Physics.Raycast(ray, out hit, Mathf.Infinity))
    10.                 {
    11.                     Debug.Log("I'm here 3");
    12.                     if (check == 0)
    13.                     {
    14.                          Debug.Log("I'm here 4");
    15.                         switch (hit.transform.tag)
    16.                         {
    can you just addd some debug logs and see how far it get's ?
    maybe its entering the frist if but doesn't go further
     
  20. GeekStories

    GeekStories

    Joined:
    Jan 22, 2015
    Posts:
    74
    Until a moment a go, it never passed the first if statement "if(shouldSelectPlanet)"
    Now it loops it once. But only once.
     
  21. UbiquitousStudio

    UbiquitousStudio

    Joined:
    Oct 11, 2015
    Posts:
    24
    Okay in this situation I would just stat a new script and simply just put a bool with the if and debug if that doesn't work alone you know it's not going to work this way. I'm almost positive you will need to switch around the input and if(shouldSelectPlanet) I'm just not 100%. The code looks like it should work, but it's just one of those things.
     
  22. GeekStories

    GeekStories

    Joined:
    Jan 22, 2015
    Posts:
    74
    Okay. Haha yeah it definitely is one of those things. Thanks for the advice :)
     
  23. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    You initialise the bool as false
    it is only set true in StarProcess()
    You never call StarProcess()
     
  24. GeekStories

    GeekStories

    Joined:
    Jan 22, 2015
    Posts:
    74
    The problem is solved however, StarProcess is called in an onClick function on a uGUI in the scene :)