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

This has been solved :slight_smile:

So this is the statement:

if(shouldSelectPlanet)
{
     //other code that works without this if statement
}

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:

if(shouldSelectPlanet == true)
{
  //other code that works without this if statement
}

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!

did you try to initialize this variable when declaring it ??? giving it a value True/false ?

Yep, I have given it a true/false value when declaring it.

Maybe Check if their is a code before the If statement blocking the if before even executing it ?

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 :face_with_spiral_eyes:

The easiest way to get help is to show the whole code, because there might be other things affecting it.

1 Like

can you post you update() code?

how about a print statement just before the if so you can see what it is at the point it’s “going wrong”…

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 :smile: thank you!

Here is the if statement and its guts WITH the function thats called when a button is clicked

void Update()
{
        print(shouldSelectPlanet);
        if (shouldSelectPlanet) //Is always false for some odd reason
        {
            //Here down WORKS if the above if statement is commented out or deleted
            if (Input.GetMouseButtonDown(0)) //Click the left mouse button
            {
                Ray ray = Camera.main.GetComponent<Camera>().ScreenPointToRay(Input.mousePosition);
                RaycastHit hit;
                if (Physics.Raycast(ray, out hit, Mathf.Infinity))
                {
                    if (check == 0)
                    {
                        switch (hit.transform.tag)
                        {
                            case "Player":
                                GameManager.news = "You can not destroy your own planet!";
                                break;
                            case "Stray":
                                selectedPlanet = hit.transform.gameObject;
                                GameManager.news = "Click this planet again to verify you wish to destroy it!";
                                check++;
                                break;
                            case "Enemy":
                                selectedPlanet = hit.transform.gameObject;
                                GameManager.news = "Click this planet again to verify you wish to destroy it!";
                                check++;
                                break;
                            default:
                                GameManager.news = "Please select a planet to destroy!";
                                break;
                        }
                    }
                    else if (check == 1 && hit.transform == selectedPlanet.transform)
                    {
                        LaunchRocketAtPlanet();
                        check = 0;
                    }
                    else
                    {
                        check = 0;
                        GameManager.news = "Please select a planet to destroy!";
                    }
                }
            }
        }
}


//Function Called When a UGUI button is clicked
public void StartProcess()
    {
        if (PlayerUnits.missiles >= 1)
        {
            if (!GameManager.isBuilding && !GameManager.isUpgrading && !GameManager.isCreatingUnits && !GameManager.isResearching)
            {
                shouldSelectPlanet = true;
                GameManager.news = "Please select a planet to destroy!";
                //Stop ALL processes
                GameManager.isLaunchingMissile = true;
            }
        }
        else
        {
            CreateMissile();
        }
    }

Or you paste your code here and get help :slight_smile:
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.

Haha yep, that has been done just before :slight_smile:
the bool for the shouldSelectPlanet is written as

bool shouldSelectPlanet = false;

as nothing but that script needs access to it.

If you want actual help, don’t discuss, post the whole script…

This is the entire script here.

using UnityEngine;
using UnityEngine.UI;

public class LaunchRocket : MonoBehaviour {

    public float timeToSelectedPlanet;
    public GameObject selectedPlanet;
    public Text missileText;
    int check = 0;
    bool shouldSelectPlanet = false;

    void Start()
    {
        missileText = GameObject.Find("LaunchRocketsText").GetComponent<Text>();
    }

    void Update()
    {
        if (PlayerUnits.missiles >= 1)
        {
            missileText.text = "Launch Rocket - " + PlayerUnits.missiles.ToString();
            missileText.GetComponentInParent<Button>().interactable = true;
        }
        else
        {
            if (MissileFactory.canCreateRockets)
            {
                missileText.GetComponentInParent<Button>().interactable = true;
                missileText.GetComponent<Text>().text = "Create a rocket";
            }
            else
            {
                missileText.GetComponentInParent<Button>().interactable = false;
                missileText.GetComponent<Text>().text = "Create a missile launch bay!";
            }
        }

        print(shouldSelectPlanet);
        if (shouldSelectPlanet)
        {
            if (Input.GetMouseButtonDown(0))
            {
                Ray ray = Camera.main.GetComponent<Camera>().ScreenPointToRay(Input.mousePosition);
                RaycastHit hit;
                if (Physics.Raycast(ray, out hit, Mathf.Infinity))
                {
                    if (check == 0)
                    {
                        switch (hit.transform.tag)
                        {
                            case "Player":
                                GameManager.news = "You can not destroy your own planet!";
                                break;
                            case "Stray":
                                selectedPlanet = hit.transform.gameObject;
                                GameManager.news = "Click this planet again to verify you wish to destroy it!";
                                check++;
                                break;
                            case "Enemy":
                                selectedPlanet = hit.transform.gameObject;
                                GameManager.news = "Click this planet again to verify you wish to destroy it!";
                                check++;
                                break;
                            default:
                                GameManager.news = "Please select a planet to destroy!";
                                break;
                        }
                    }
                    else if (check == 1 && hit.transform == selectedPlanet.transform)
                    {
                        LaunchRocketAtPlanet();
                        check = 0;
                    }
                    else
                    {
                        check = 0;
                        GameManager.news = "Please select a planet to destroy!";
                    }
                }
            }
        }
    }

    public void LaunchRocketAtPlanet()
    {
        timeToSelectedPlanet = Vector3.Distance(GameObject.Find("PlayersPlanet").transform.position, selectedPlanet.transform.position);
        timeToSelectedPlanet = Mathf.RoundToInt(timeToSelectedPlanet);
        GameManager.news = "In " + timeToSelectedPlanet.ToString() + " you will destroy the planet " + selectedPlanet.GetComponent<PlanetController>().planetName + "!";
        GameObject missile = Instantiate(Resources.Load("Missile"), transform.position, Quaternion.identity) as GameObject;
        missile.GetComponent<Missile>().targetPlanet = selectedPlanet;
        PlayerUnits.missiles -= 1;
        GameManager.isLaunchingMissile = false;
        shouldSelectPlanet = false;

        selectedPlanet.gameObject.tag = "TARGET";
    }

    public void StarProcess()
    {
        if (PlayerUnits.missiles >= 1)
        {
            if (!GameManager.isBuilding && !GameManager.isUpgrading && !GameManager.isCreatingUnits && !GameManager.isResearching)
            {
                shouldSelectPlanet = true;
                GameManager.news = "Please select a planet to destroy!";
                //Stop ALL processes
                GameManager.isLaunchingMissile = true;
            }
        }
        else
        {
            CreateMissile();
        }
    }

    void CreateMissile()
    {
        if (GameObject.Find("PlayersPlanet").GetComponent<PlanetController>().metal >= 1500 &&
            GameObject.Find("PlayersPlanet").GetComponent<PlanetController>().water >= 800 &&
            GameObject.Find("PlayersPlanet").GetComponent<PlanetController>().lightPoints >= 10)
        {
            PlayerUnits.missiles++;

            GameObject.Find("PlayersPlanet").GetComponent<PlanetController>().lightPoints -= 10;
            GameObject.Find("PlayersPlanet").GetComponent<PlanetController>().water -= 800;
            GameObject.Find("PlayersPlanet").GetComponent<PlanetController>().metal -= 1500;
        }
    }
}

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./

I have tried this and still nothing.
and I use bools like this in if statements in other scripts perfectly fine too :wink:

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.

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.

        if (shouldSelectPlanet)
        {
            Debug.Log("I'm here 1");
            if (Input.GetMouseButtonDown(0))
            {
                Debug.Log("I'm here 2");
                Ray ray = Camera.main.GetComponent<Camera>().ScreenPointToRay(Input.mousePosition);
                RaycastHit hit;
                if (Physics.Raycast(ray, out hit, Mathf.Infinity))
                {
                    Debug.Log("I'm here 3");
                    if (check == 0)
                    {
                         Debug.Log("I'm here 4");
                        switch (hit.transform.tag)
                        {

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

Until a moment a go, it never passed the first if statement “if(shouldSelectPlanet)”
Now it loops it once. But only once.