I am currently working on some inventory scripts and I was wondering is there a method for disabling individual GUI labels(use an item in the inventory) other than possibly making a whole other script and disabling it when that object gets used ?
Any suggestion or comments would be much appreciated
var ThisLabelEnabled = false;
var ThatLabelEnabled = true;
function OnGUI () {
GUI.enabled = ThisLabelEnabled;
GUI.Label(Rect(10,10,40,20), "This");
GUI.enabled = ThatLabelEnabled;
GUI.Label(Rect(10,40,40,20), "That");
GUI.enabled = true;
}
Done! Just use individual state variables of some sort and you’re golden.
Thanks
i thought that GUI.enable disabled the whole GUI not individual elements.
Yeah, not quite. When you set GUI.enabled it’s then set for all elements drawn after that setting. So you can set it, draw some elements, reset it and draw some more elements, etc. That of course is also why at the end you probably want to make sure you set it back to true otherwise other OnGUI functions may unexpected draw disabled elements! 
Unfortunately this doesn’t seem to do anything at all… I have tried with both a bool variable and just putting GUI.enabled=false over the item with no results. Here is the code that effects what I am doing at the moment.
//GUI controll
public bool spillkitsmall=false;
public bool spillbucketsmall=false;
public bool spillbroomsmall=false;
//GUI
void OnGUI()
{
GUI.enabled = spillkitsmall;
GUI.Label(label1, spillkit);
GUI.enabled = spillbucketsmall;
GUI.Label(label2, spillbroom);
GUI.enabled = spillbroomsmall;
GUI.Label(label3, spillbucket);
}
All GUI labels with this are all enabled when I run.
Any ideas?
Yeah, are you certain you haven’t set those public variables to true in the Inspector? Using a slightly tweaked version of your code works just fine for me:
using UnityEngine;
using System.Collections;
public class Foo : MonoBehaviour {
//GUI controll
public bool spillkitsmall=false;
public bool spillbucketsmall=false;
public bool spillbroomsmall=false;
//GUI
void OnGUI()
{
GUI.enabled = spillkitsmall;
GUI.Label(new Rect(10,10,50,20), "spillkit");
GUI.enabled = spillbucketsmall;
GUI.Label(new Rect(10,40,50,20), "spillbroom");
GUI.enabled = spillbroomsmall;
GUI.Label(new Rect(10,70,50,20), "spillbucket");
}
}
I hard-coded the rects and label string values, but used the variables for enabled states just fine.
You know some times Iv found these things to fix themselves by simply reworking the code from the ground up. Ill go though and do that Monday, I did however after my last post make all the bool variables private having unintentionally making them public. But this didn’t make much of a difference, probably something small I am missing that ill find when i do it again. Thank You HiggyB ill post my results monday
OH i see it just simply dims them out I was under the impression that it made them disappear all together, I guess I could accomplish this by replacing the label with an empty one.
Thanks
Yup, as per the docs (GUI.enabled), disabling draws the controls, dimmed out but inactive. If you don’t want them drawn at all then it’s a mild tweak to the code:
using UnityEngine;
using System.Collections;
public class Foo : MonoBehaviour {
//GUI controll
public bool spillkitsmall=false;
public bool spillbucketsmall=false;
public bool spillbroomsmall=false;
//GUI
void OnGUI()
{
if (spillkitsmall) { GUI.Label(new Rect(10,10,50,20), "spillkit"); }
if (spillbucketsmall) { GUI.Label(new Rect(10,40,50,20), "spillbroom"); }
if (spillbroomsmall) { GUI.Label(new Rect(10,70,50,20), "spillbucket"); }
}
}
Done!
Thank You
I actually achieved what I was looking for by making prefabs of empty GameObjects and attaching the individual item scripts to them now when i pick up an item it appears in my inventory. When I use the item i simply destroy it. This is a more dynamic approach I wanted. Thank you again for your feed back.