script on object being ignored

so i have a script that does a few things currently, and it’s attached to an object in the scene. the problem is, neither the GUI button functions nor the code in update are running (nor the very simple code in start). i swear i’ve used this code before and it worked, so i don’t know what the problem is.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace CoS
{
    public class heroInventory : MonoBehaviour
    {
        public Canvas hinventorycanvas;
        public Button xbutton;
        private int invcapacity = 50;
        public string[,] inventory;

        public Canvas hinvequippedcanvas;
        public Button inviconbtn;

        // Use this for initialization
        void Start()
        {
            hinventorycanvas.enabled = false;
            hinvequippedcanvas.enabled = false;
            string[,] inventory = new string[invcapacity, 16];
        }

        // Update is called once per frame
        void Update()
        {
            if (Input.GetKeyDown(KeyCode.I))
            {
                hinvequippedcanvas.enabled = !hinvequippedcanvas.enabled;
            }

        }
       
        private void OnGUI()
        {
            if (xbutton)
            {
                hinventorycanvas.enabled = false;
            }
            //if inventory button pressed
            if (inviconbtn)
            {
                hinventorycanvas.enabled = true;
                //ShowInventory();
            }
        }
        // got to look at GM to figure out the types for the inventory
        /*void ShowInventory()
        {
            string labeltxt = "";
            string labelimg = "";
            int counter = 0;
            for (int i = 0; i < invcapacity; i++)
            {
                counter = i + 1;
                //update icon and text
                labeltxt = "Text" + counter;
                labelimg = "Image" + counter;

                Text text = GameObject.Find(labeltxt).GetComponent("Text") as Text;
                text.text = inventory[i, 1] + " x " + inventory[i, 2];    // Need to fill out the inventory
                Image image = GameObject.Find(labelimg).GetComponent("Image") as Image;
                Sprite invicon = Resources.Load<Sprite>(inventory[i, 3]);
                image.sprite = invicon;
            }
        }*/
    }
}

The object is active in the scene? Did you also put a debug or print in the Update (before the if) to make sure your code isn’t working?

1 Like

You are mixing GIU and UI, also you are just check if a button exists, not if it was pressed. Basically that whole last section is nonsense code, it won’t behave like you expect.
Start here:

There are examples that will help.

2 Likes

any idea why the “press i” code doesn’t work?

You are turning it off every frame in the ongui

1 Like

weird, i changed everything and had it right, but it wouldn’t work. then suddenly it started working…kind of weird.

I would just totally ditch the OnGUI and enable/disable whatever component/game object you need, from Update (and the UI elements) :slight_smile: Just a thought.

1 Like

yeah, that’s what i did. i reminded myself how to do buttons by watching a tutorial. so i wrote two functions, one for each button, and linked them in the editor. the “press i” code wasn’t working first for the reason gorilla mentioned, then because i was starting out the scene with the two canvases disabled. so anyway i got all this working. now i’m trying to figure out how to do inventory architecture…