Support => NGUI 3 Support => Topic started by: redofpaw on January 06, 2014, 08:35:19 AM
Title: Disable button script
Post by: redofpaw on January 06, 2014, 08:35:19 AM
Hey folks,
Working on a Tower Defence game and I want to 'grey out' the buttons if the player cash is too low. I can see a way to have a central 'player cash' variable located on a management object.
How would I then ensure the correct button in the UI is disabled based on the value of this variable (if 'cash' < 'tower value' then button disabled)?
I can apply an additional script to the button, but how does that script then tell the button to disable?
Coding in C#.
Thanks for any help!
Title: Re: Disable button script
Post by: ArenMook on January 06, 2014, 10:38:44 AM
button.isEnabled= yourCash >123;
Title: Re: Disable button script
Post by: redofpaw on January 06, 2014, 10:55:18 AM
@ArenMook
Thanks - so I think I need to edit the actual 'UIButton' script, rather than add another script, is that right?
I tried your code, in a variety of places but the most I got was a malfunctioning button. Where exactly and in what context should it be placed so that it greys out the button (as in: it is a 'diabled' button from the point of view of NGUI).
Thanks again,
Title: Re: Disable button script
Post by: ArenMook on January 06, 2014, 10:59:46 AM
Eh? No. My code assumes that 'button' is a UIButton reference, and 'yourCash' is some variable of yours. It's pseudocode. UIButton has 'isEnabled' function that can enable/disable it, thus graying it out if you've set the disabled color accordingly.
Title: Re: Disable button script
Post by: redofpaw on January 06, 2014, 11:06:34 AM
Ah, wait I think I figured out what I need to do. Before writing this I wasn't sure I should be editing the UI Button script, but I see now where to alter it to get this working.
Thanks for your help.
Title: Re: Disable button script
Post by: redofpaw on January 06, 2014, 11:24:53 AM
Ok, so in theory that works, but now my new problem:
I want to add a public gameObject reference - this being the object I have the variableHolder script attached to in order to track player cash. However it will not show up as a field on the editor (UIButton script) to be able to 'drag and drop' the variableHolder object onto.
Is there a way, without using 'find object' to reference another gameObject on the UIButton script in this way?
Am I missing something simple and obvious?
EDIT: WAIT! This is even more confusing. Editing the UIButton script edits the script for ALL the buttons (of course). It seems like a bad idea for me to even be editing this script, yet how do I set 'isEnabled' = false on the UIButton script from a different script attached to the button. I am not particuarly experienced in coding, so if this is VERY simple and OBVIOUSLY obvious, then perhaps someone can explain it like I'm 5?
Title: Re: Disable button script
Post by: ArenMook on January 06, 2014, 11:57:31 AM
Ok, here's the full script for you. You should never modify NGUI's files unless you're an expert.
usingUnityEngine;
publicclass Test : MonoBehaviour
{
publicint myValue =50;
void Update()
{
UIButton btn = GetComponent<UIButton>();
btn.isEnabled=(myValue >50);
}
}
Title: Re: Disable button script
Post by: redofpaw on January 06, 2014, 12:44:33 PM
Thanks ArenMook! Makes a lot more sense now :)
I'm still learning, so some obvious scripting is lost on me.