Where exactly am I making the error?

If there is a gameobject I want to find with a tag, I want it to run the “if” command if it appears in the hierarchy. If it does not, I want it to run the “else if” command, but whatever I did, I could not run the “if” command.

    void Update()
    {
        RewardSystem();
    }

public void RewardSystem()
    {
        if (GameObject.FindGameObjectWithTag(cube_Tag_Name) == null)
        {
            if (GameObject.FindGameObjectWithTag(cube_Tag_Name) == true)
            {
                SoundManager.instance.NextLevelMenuSound();
                next_Level_Menu.SetActive(true);
                confetti.Play();

                game_Over_Menu.SetActive(false);
                clickButton.gameObject.SetActive(false);
                missionButton.gameObject.SetActive(false);
                enabled = false;
            }

            else if (GameObject.FindGameObjectWithTag(cube_Tag_Name) == false)
            {
                SoundManager.instance.GameOverMenuSound();
                game_Over_Menu.SetActive(true);

                next_Level_Menu.SetActive(false);
                confetti.Stop();
                clickButton.gameObject.SetActive(false);
                missionButton.gameObject.SetActive(false);

                enabled = false;
            }
        }
    }

Links if images do not open :

  1. https://ibb.co/bKC48F6
  2. https://ibb.co/KKhzbZW


What error?

How to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

How to understand compiler and other errors and even fix them yourself:

https://discussions.unity.com/t/824586/8

I have added a more detailed explanation.

Please help me here, what does this mean?

  • you are unable to type “if” ?
  • the if command doesn’t compile?
  • the if command is never executed?
  • when you run it, it does not give you the expected results?

You have to be EXTREMELY specific. None of us here live inside your head.

To begin with, break apart stuff like this and assign it to temporary variables:

Such as:

bool FoundCube = GameObject.FindGameObjectWithTag(cube_Tag_Name);

Now you can print FoundCube using Debug.Log() and see if THAT is your problem.

This is how you debug a program.

In fact, to help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run?
  • what are the values of the variables involved? Are they initialized?

Knowing this information will help you reason about the behavior you are seeing.

Sir my English is not very good, I try to explain myself as much as I can. I am sorry about this.

I used Debug.Log () and if doesn’t work only else if works. I’m asking for help because I can’t understand why if doesn’t work.

When the gameobject tag linked to the name cube_Tag_Name is active in the game, it does not run if, but when the gameobject tag linked to the cube_Tag_Name name is not active in the game, it runs if else.

I hope it’s more revealing to you.

This is the problem: read your logic carefully in the code above:

if line 9 is true, then line 11 will NEVER be true because it is inside of line 9

Maybe remove the line 9 if completely and only use the other 2 if statements??