My UI is not working and at one point I thought it was.

Thank you to anyone reading this, I’m having great difficulty figuring out why all of a sudden my code isn’t working. To start, I’m not the best at programming and so I apologize if my script looks very ugly to those that are experts in understanding and reading code.

So I intially had it working, or so I thought. The video below shows it to be working, but I noticed later that the executable I made around that time showed it wasn’t.

Below this text is my most recent video and it shows that the first UI is displaying because I have it programmed in void start() to equal true. The code works intially beause of that, in which the player can’t move until after they press ‘0’. My triggers do seem to be working because when the player walks towards the moveable box, I have a debug line that appears in my Console that is showing that player is near the box.

Below this text I will post my code as well as a screenshot showing that I have my InstructionsUI placed within the Canvas object of the Heirarchy and the order of the texts displaying within the Inspector. Also in the screenshot it will show that I do have my C# script placed inside of my FPS Standard Assets because I learned previously that was the only way my FirstPersonController script would have access to my script for InstructionsUI.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.Characters.FirstPerson;

public class InstructionsUI : MonoBehaviour
{
    public GameObject boxForText;
    private bool makeBoxAppear;
    private bool bufferBetweenTexts;
    public static bool allowToWalk;
   // public static float speedOfWalk;

    public GameObject introText1;
    public GameObject introBox2;
    public GameObject introPickaxe3;
    public GameObject introCrystals4;
    public GameObject introMachine5;
    public GameObject introRunning6;
    public GameObject introSpace7;
    public GameObject warning7;

    private bool nearTheBox;
    private bool nearThePickaxe;
    private bool nearCrystals;
    private bool isNearMachine;
    private bool nearSpace;
    private bool playerCanRun;

    private bool firstInstruct;
    private bool secondInstruct;
    private bool thirdInstruct;
    private bool fourthInstruct;
    private bool fifthInstruct;
    private bool sixthInstruct;
    private bool seventhInstruct;

    // Start is called before the first frame update
    void Start()
    {
        boxForText.SetActive(true);
        makeBoxAppear = true;
        //bufferBetweenTexts = false;
        allowToWalk = false;
        //speedOfWalk = 0f;


        //Boolean values in order of how the instructions are given to player
        firstInstruct = false;
        secondInstruct = false;
        thirdInstruct = false;
        fourthInstruct = false;
        fifthInstruct = false;
        sixthInstruct = false;
        seventhInstruct = false;

    //Game objects that hold the text and are only true based on boolean values above
        introText1.SetActive(true);
        introBox2.SetActive(false);
        introPickaxe3.SetActive(false);
        introCrystals4.SetActive(false);
        introMachine5.SetActive(false);
        introRunning6.SetActive(false);
        introSpace7.SetActive(false);
        warning7.SetActive(false);
  
      
    }

    // Update is called once per frame
    void Update()
    {
        nearTheBox = FirstPersonController.nearBox;
        //nearThePickaxe = FirstPersonController.hasPickaxe;
        nearThePickaxe = pickaxeHold.allowedToSwing;
        nearCrystals = FirstPersonController.nearHarvestCrystals;
        isNearMachine = FirstPersonController.nearMachine;
        nearSpace = FirstPersonController.isNearSpace;
        playerCanRun = FirstPersonController.runInstructions;
      
        // allowToWalk = FirstPersonController.playerCanWalk;
        //speedOfWalk = FirstPersonController.m


        //Make intro go away
        if (Input.GetKeyDown(KeyCode.Alpha0) && firstInstruct == false)
        {
            boxForText.SetActive(false);
            introText1.SetActive(false);
            firstInstruct = true;
            makeBoxAppear = false;
            allowToWalk = true;
            //speedOfWalk = 6f;

        }
        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        //Bring up second introduction when player is near the box
            if (firstInstruct == true && nearTheBox == true && makeBoxAppear == false && secondInstruct == false)
        {
            boxForText.SetActive(true);
            introBox2.SetActive(true);
            makeBoxAppear = true;
            allowToWalk = false;
            //speedOfWalk = 0f;
        }

        //Player presses 0 to make second text to disappear
        if (Input.GetKeyDown(KeyCode.Alpha0) && secondInstruct == false && makeBoxAppear == true)
        {
            makeBoxAppear = false;
            secondInstruct = true;
            boxForText.SetActive(false);
            introBox2.SetActive(false);
            allowToWalk = true;
            //speedOfWalk = 6f;
        }

        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        //Bring up third introduction when player collides with pickaxe
        if (secondInstruct == true && nearThePickaxe == true && makeBoxAppear == false && thirdInstruct == false)
        {
            boxForText.SetActive(true);
            introPickaxe3.SetActive(true);
            makeBoxAppear = true;
            allowToWalk = false;
            // speedOfWalk = 0f;
        }

        //Player presses 0 to make third text to disappear
        if (Input.GetKeyDown(KeyCode.Alpha0) && thirdInstruct == false && makeBoxAppear == true)
        {
            makeBoxAppear = false;
            thirdInstruct = true;
            boxForText.SetActive(false);
            introPickaxe3.SetActive(false);
            allowToWalk = true;
            //speedOfWalk = 6f;
        }
        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
      
        //Bring up fourth introduction when player is near crystals
        if (thirdInstruct == true && nearCrystals == true && makeBoxAppear == false && fourthInstruct == false)
        {
            boxForText.SetActive(true);
            introCrystals4.SetActive(true);
            makeBoxAppear = true;
            allowToWalk = false;
        }
        //Player presses 0 to make fourth text to disappear
        if (Input.GetKeyDown(KeyCode.Alpha0) && fourthInstruct == false && makeBoxAppear == true)
        {
            makeBoxAppear = false;
            fourthInstruct = true;
            boxForText.SetActive(false);
            introCrystals4.SetActive(false);
            allowToWalk = true;
        }
        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        //Bring up fifth introduction when player is near crystals
        if (fourthInstruct == true && isNearMachine == true && makeBoxAppear == false && fifthInstruct == false)
        {
            boxForText.SetActive(true);
            introMachine5.SetActive(true);
            makeBoxAppear = true;
            allowToWalk = false;
        }

        //Player presses 0 to make fifth text to disappear
        if (Input.GetKeyDown(KeyCode.Alpha0) && fifthInstruct == false && makeBoxAppear == true)
        {
            makeBoxAppear = false;
            fifthInstruct = true;
            boxForText.SetActive(false);
            introMachine5.SetActive(false);
            allowToWalk = true;
        }
        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
      
        //Bring up sixth introduction when player is near running
        if (fifthInstruct == true && playerCanRun == true && makeBoxAppear == false && sixthInstruct == false)
        {
            boxForText.SetActive(true);
            introRunning6.SetActive(true);
            makeBoxAppear = true;
            allowToWalk = false;
        }

        //Player presses 0 to make sixth text to disappear
        if (Input.GetKeyDown(KeyCode.Alpha0) && sixthInstruct == false && makeBoxAppear == true)
        {
            makeBoxAppear = false;
            sixthInstruct = true;
            boxForText.SetActive(false);
            introRunning6.SetActive(false);
            allowToWalk = true;
        }

        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        //Bring up seventh introduction when player is near space
        if (sixthInstruct == true && nearSpace == true && makeBoxAppear == false && seventhInstruct == false)
        {
            boxForText.SetActive(true);
            introSpace7.SetActive(true);
            warning7.SetActive(true);
            makeBoxAppear = true;
            allowToWalk = false;
        }

        //Player presses 0 to make seventh text to disappear
        if (Input.GetKeyDown(KeyCode.Alpha0) && seventhInstruct == false && makeBoxAppear == true)
        {
            makeBoxAppear = false;
            seventhInstruct = true;
            boxForText.SetActive(false);
            introSpace7.SetActive(false);
            warning7.SetActive(false);
            allowToWalk = true;
        }
    }
}

Here is the screenshot as I mention above showing the Heirarchy, my Project files within my Standard Assets of my FPS Controller and the Inspector showing how the objects within the Heirarchy are all hooked up in order of their number.

Thank you so much for your time in helping me with this. Any help is very appreciated.

Sorry, what is your question?

I just don’t know why that when I go near the box and the debug line reads that the trigger is working, and despite all the conditions being met in order for the first instructions to appear, it still doesn’t show my UI and the player is still being allowed to move. So my question is why in my code for the first instructions to work that follows my if statement below not working?

So this line of code should be making the second text appear:

 if (firstInstruct == true && nearTheBox == true && makeBoxAppear == false && secondInstruct == false)
        {

To clarify, makeBoxAppear == false means that my UI box isn’t displaying and when it is true the UI box appears around the text. nearTheBox == true is my trigger for the actual object/box in the game.

You will want to debug it and find which variable probably doesn’t have the value that you expect https://discussions.unity.com/t/748729/14

So I placed debug lines to see if any of the conditions are changing, and for some reason they’re not being updated…
The only one that is updated is the line Debug.Log(("first Instruct: ") + (firstInstruct)); because as seen in my script I made it appear under the condition of when I press ‘0’.
My debug on the trigger for the box “is near box” is being triggered but its not updating the line of code nearTheBox = FirstPersonController.nearBox; as you can see in the Console it shows “nearTheBox = false;” when it should be readings as true if “is near box” is being triggered.

https://drive.google.com/file/d/1VYRf7p_tlvKf5uQqg6GKhYIDUjhcCYvr/view?usp=sharing

I’ll be honest, it’s hard to follow your script. I’d also say that you’re probably going to run into a lot of issues the way you have it written, especially since you’re trying to track a lot of different bool values and doing if checks on them.

I believe you’d be better off with a state machine to control what state of the tutorial the user is in. An enum that tracks what step you are on might help control things a little better with the state machine.

That being said, it’d be helpful to know how you’re setting your bool values, which seem to be static variables of FirstPersonController I’m guessing?

We don’t see the script where you use OnTriggerEnter. Or is it in the google drive? It’s better to post it here. Doubt anyone clicks on some random googledrive file links here. :wink:

Yeah I truly wish I was more familiar with enums and state machines. I haven’t messed with that for probably 2 years now when I took a CS class during that time. Yes, the bool values within the FirstPersonController are set to static. If you have any good recommendations on how to learn enums and state machines quickly enough for me to implement that sort of thing tonight then I’d appreciate it. I guess I was just hoping it would be an easy fix since at some point it appeared to be working, and the issue seems to be that my void update() doesn’t want to update every frame…

I could post my FirstPersonController script but I don’t think that is the problem since my debugs are showing the triggers are working and I have it working in other places without any issues. Only reason I didn’t want to post that is because I’ve adapted someone elses code and it’s quite a big script. I’ll go ahead and break down the important parts that are relevant to the subject so that its easier to examine. Also I didn’t know how else to post an image because it asks for a link when I click to add an image. Please let me know if there is a better way than using Google Drive.
Here are the variables:

        //My code for collecting crystals
        public static float count;

        //Colliding with machinary
        public static bool nearMachine;

        //Allow platform to now move
        public static bool cavePlatformMove;

        //Messing with Players gravity in outerspace
        private static bool useGravity;
        public static bool isNearSpace;
        //[SerializeField] private float m_gravityOff;
        // public static float m_GravityMultiplier;

        //Pickaxe variables
        public static bool hasPickaxe;
        public static bool nearHarvestCrystals;

        //Push and pull boxes
        public static bool nearBox;
        public static bool playerJumps;

        public static bool runInstructions;

        public static bool openDoors1;


        // Use this for initialization
        private void Start()
        {
            count = 0f;
            nearMachine = false;
            cavePlatformMove = false;
            useGravity = false;
            hasPickaxe = false;
            nearHarvestCrystals = false;
            isNearSpace = false;
            runInstructions = false;

            m_CharacterController = GetComponent<CharacterController>();
            m_Camera = Camera.main;
            m_OriginalCameraPosition = m_Camera.transform.localPosition;
            m_FovKick.Setup(m_Camera);
            m_HeadBob.Setup(m_Camera, m_StepInterval);
            m_StepCycle = 0f;
            m_NextStep = m_StepCycle / 2f;
            m_Jumping = false;
            m_AudioSource = GetComponent<AudioSource>();
            m_MouseLook.Init(transform, m_Camera.transform);

            
        }

Here is the OnTriggerEnter and the OnTriggerExit:

 private void OnTriggerEnter(Collider other)
        {
            if (other.gameObject.CompareTag("PushPullTag"))
            {
                nearBox = true;
                Debug.Log("Is near box");
            }

            //My code for crystals
            if (other.gameObject.CompareTag("Pick Up"))
            {
                //other.gameObject.SetActive (false);
                Destroy(other.gameObject);
            }

            //Check if colliding with machine
            if (other.gameObject.CompareTag("NearMachine") && (count >= 1f))
            {
                nearMachine = true;
                count = count - 1f;
                cavePlatformMove = true;
            }

            //Check if near the air lock and open doors if so
            if (other.gameObject.CompareTag("openDoors1"))
            {
                openDoors1 = true;
            }

            //Check if near the huge crystals in cave
            if (other.gameObject.CompareTag("nearCrystalHarvest"))
            {
                nearHarvestCrystals = true;
            }

            //Check if colliding with spinning pickaxe that player can pick up
            if (other.gameObject.CompareTag("spinPickaxe"))
            {
                Destroy(other.gameObject);
                hasPickaxe = true;
            }

            if (other.gameObject.CompareTag("InstructRun"))
            {
                runInstructions = true;
            }

            //Messing with Player's gravity as OFF
            if(other.gameObject.CompareTag("GravityOff"))
            {
                useGravity = false;
                m_GravityMultiplier = 1f;
             
            }
            //Messing with Player's gravity as ON
            if (other.gameObject.CompareTag("GravityOn"))
            {
                useGravity = true;
                isNearSpace = true;
                m_GravityMultiplier = 2f;
              
               
            }

            if (other.gameObject.CompareTag("killZone"))
            {
                SceneManager.LoadScene(SceneManager.GetActiveScene().name);
            }

        }

        private void OnTriggerExit(Collider other)
        {
            if(other.gameObject.CompareTag("NearMachine"))
            {
                nearMachine = false;
            }

            if (other.gameObject.CompareTag("nearCrystalHarvest"))
            {
                nearHarvestCrystals = false;
            }

            if (other.gameObject.CompareTag("PushPullTag"))
            {
                nearBox = false;
            }

        }


    }
}

Thanks to all helping, I’m sorry I’m not great at programming and most my experince comes from working with GameMaker2 with their built in GML language… I’m more or less a level/game design student :sweat_smile:

Since these are statics and it can’t really be a wrong instance problem or something like that somewhere your scripts probably change nearBox to false again before the update of the other script runs.
Search all your scripts for “nearBox = false;” and put debugs there to see where the variable gets changed. Or just make a static Set method for this variable and put one debug there and don’t allow any script to change the variable directly.

Thank you for the advice, however that doesn’t seem to be the issue. I’ll have to look into it further, but I feel that its because my void Update() isn’t updating as it should. Anyways thanks for the suggestion nonetheless.

Not really possible unless the game object with the script is not active or the script not enabled. Do you maybe have two FirstPersonController scripts in different namespaces?

So I found the solution, although I truly still don’t understand why it works in the way I solved it. What I did was I moved the script to the canvas object directly instead of having the script attached to the child of the canvas object.

I want to thank you and everyone else for replying with solutions to my post. I really appreaciate how helpful everyone is in this community/unity forum. Really though, thank you thank you thank you. <3

https://www.youtube.com/watch?v=nTpe3HoASRs