Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Game Development

Elijah Franklin
Elijah Franklin
882 Points

If Statements not Triggering after One Another.

I have an issue. I have two 'If' Statements running in an update function. However, I noticed that the If statement that is placed after the first If Statement doesn't execute it's code.

// currently in an update Function.

// This one works fine. v
        if (GameObject.FindGameObjectWithTag("Put1").transform.parent != null && Input.GetKey(KeyCode.E))
        {
            Item1();
        }

// This one does not. v
        if (GameObject.FindGameObjectWithTag("Put2").transform.parent != null && Input.GetKey(KeyCode.E))
        {
            Item2();
        }

I have swapped the order of both the if statements, and long story short, both of the if statements do exactly what I want them to do. Is there something about the order of If statements in code that I don't know about? Thanks!

Elijah Franklin
Elijah Franklin
882 Points

what script is this in i maybe able to help

The script is just an empty game object in the hierarchy, and it is in C#.

6 Answers

Elijah Franklin
Elijah Franklin
882 Points

SO I figured it out. The thing that fixed the problem, was that I simply removed an unnecessary piece of code and it works now!

if (Input.GetKey(KeyCode.E) && GameObject.FindGameObjectWithTag("Put1"))
        {
            Item1();
        }


        if (Input.GetKey(KeyCode.E) && GameObject.FindGameObjectWithTag("Put2"))
        {
            Item2();
        }
Tojo Alex
Tojo Alex
Courses Plus Student 13,331 Points

congrats Elijah you didn't really need me. I'll see you later, Tojo

Tojo Alex
PLUS
Tojo Alex
Courses Plus Student 13,331 Points

ok i just need to know is this is all the code in that script if so please show it then i can help you out since it might not be the if statement that is causing the problem

Elijah Franklin
Elijah Franklin
882 Points

ok i just need to know is this is all the code in that script if so please show it then i can help you out since it might not be the if statement that is causing the problem

Here is the entire Script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class IM : MonoBehaviour
{
    public GameObject mashroom;
    public GameObject player;
    public MashroomFullGrown MFG;
    public GameObject Slot1;
    public Slot1 S1;
    public GameObject Slot2;
    public Slot2 S2;
    public GameObject Slot3;
    public Slot3 S3;
    public GameObject Slot4;
    public Slot4 S4;
    public GameObject Slot5;
    public Slot5 S5;
    public GameObject Slot6;
    public Slot6 S6;
    public float take;
    public float delay;
    // Use this for initialization


    void Start()
    {
        GameObject q = GameObject.FindGameObjectWithTag("S1");
        S1 = q.GetComponent<Slot1>();
        GameObject w = GameObject.FindGameObjectWithTag("S2");
        S2 = w.GetComponent<Slot2>();
        GameObject e = GameObject.FindGameObjectWithTag("S3");
        S3 = e.GetComponent<Slot3>();
        GameObject r = GameObject.FindGameObjectWithTag("S4");
        S4 = r.GetComponent<Slot4>();
        GameObject t = GameObject.FindGameObjectWithTag("S5");
        S5 = t.GetComponent<Slot5>();
        GameObject y = GameObject.FindGameObjectWithTag("S6");
        S6 = y.GetComponent<Slot6>();
    }
    public void Item1()
    {
        if (S1.item == 0)
        {
            S1.item = 1;
            delay = .5f;
        }
        else
        {
            if (delay <= 0 && S2.item == 0)
            {
                S2.item = 1;
                delay = 1f;
            }
            else
            {
                if (delay <= .5f && S3.item == 0 && S2.item > 0)
                {
                    S3.item = 1;
                    delay = 1.5f;
                }

                else
                {
                    if (delay <= 1f && S4.item == 0 && S3.item > 0)
                    {
                        S4.item = 1;
                        delay = 2f;
                    }
                    else
                    {
                        if (delay <= 1.5f && S5.item == 0 && S4.item > 0)
                        {
                            S5.item = 1;
                            delay = 2.5f;
                        }
                        else
                        {
                            if (delay <= 2f && S6.item == 0 && S5.item > 0)
                            {
                                S6.item = 1;
                            }
                        }
                    }
                }
            }
        }
    }
    public void Item2()
    {
        if (S1.item == 0)
        {
            S1.item = 2;
            delay = .5f;
        }
        else
        {
            if (delay <= 0 && S2.item == 0)
            {
                S2.item = 2;
                delay = 1f;
            }
            else
            {
                if (delay <= .5f && S3.item == 0 && S2.item > 0)
                {
                    S3.item = 2;
                    delay = 1.5f;
                }

                else
                {
                    if (delay <= 1f && S4.item == 0 && S3.item > 0)
                    {
                        S4.item = 2;
                        delay = 2f;
                    }
                    else
                    {
                        if (delay <= 1.5f && S5.item == 0 && S4.item > 0)
                        {
                            S5.item = 2;
                            delay = 2.5f;
                        }
                        else
                        {
                            if (delay <= 2f && S6.item == 0 && S5.item > 0)
                            {
                                S6.item = 2;
                            }
                        }
                    }
                }
            }
        }

    }
    void Update()
    {
        if (delay >= 0f)
        {
            delay -= Time.deltaTime;
        }

// currently in an update Function.

// This one works fine. v
        if (GameObject.FindGameObjectWithTag("Put1").transform.parent != null && Input.GetKey(KeyCode.E))
        {
            Item1();
        }

// This one does not. v
        if (GameObject.FindGameObjectWithTag("Put2").transform.parent != null && Input.GetKey(KeyCode.E))
        {
            Item2();
        }
    }
}
Tojo Alex
PLUS
Tojo Alex
Courses Plus Student 13,331 Points

Sorry for asking loads of questions but can you tell me what was the error in the unity editor (or whatever your using for your project) console.

Elijah Franklin
Elijah Franklin
882 Points

It is perfectly fine to ask questions. I realize I did not put enough information, so it is my fault. Anyway, the that error pops up is: NullReferenceException: Object reference not set to an instance of an object IM.Update () (at Assets/Scripts/UI/Slots/IM.cs:149). I never thought much of it, and thought it to be a bug of some kind as the first part worked, and kinda dismissed it. (oops) Now being as the two if statements were in the update function, the error was appearing every frame until I swapped the code around like so,

// This one works fine. v
        if (Input.GetKey(KeyCode.E) && GameObject.FindGameObjectWithTag("Put1").transform.parent != null)
        {
            Item1();
        }

// This one does not. v
        if (Input.GetKey(KeyCode.E) && GameObject.FindGameObjectWithTag("Put2").transform.parent != null)
        {
            Item2();
        }

(I didn't change anything else.) After i swapped the code, the error would only pop up whenever one of the needs for the if statement to trigger were not met. I think I figured out the problem, but I don't know the solution. Any ideas?

Tojo Alex
PLUS
Tojo Alex
Courses Plus Student 13,331 Points

This is a very complex problem indeed i am trying all the best i can do to help but i don't always have the time to figure out. Sorry. Look for any letters that need to be capitalized and check your script thoroughly especially the if statement. Don't worry I am looking for a solution and you can always ask other teachers,moderators and students. Good luck.

Tojo Alex
PLUS
Tojo Alex
Courses Plus Student 13,331 Points

More advice click on the error in the unity editor than it shall bring you to the line(s) that the error is in