Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: Divya Talluri on October 30, 2012, 08:14:10 AM

Title: Making UIButton Invisible
Post by: Divya Talluri on October 30, 2012, 08:14:10 AM
Hi Aren,

           How to make UIButton invisible? May be this is simple question, as I am new to NGUI struggling with this.I just want to make Button not included in UI,but whenever needed i want to make it visible and invisible so i cant use  nguitools.destroy method becoz it makes uibutton permanantly removed from UI.

Thanks in advance.

Title: Re: Making UIButton Invisible
Post by: ArenMook on October 30, 2012, 08:14:44 AM
NGUITools.SetActive(buttonGameObject, false).
Title: Re: Making UIButton Invisible
Post by: Divya Talluri on October 30, 2012, 08:44:15 AM
ty for the fast reply, it worked while making invisible but when i want to make it visible,for making visible i am getting button game object,then it shows error like nullobjreference  :(
Title: Re: Making UIButton Invisible
Post by: JRoch on October 30, 2012, 11:00:59 AM
You have to create a GameObject variable and store a reference to the button you are disabling.  Once disabled, the "Find" functions of Unity cannot find those game objects.  For performance reasons you should be doing this anyhow.

I've posted up example code for tracking gameobjects here:  http://www.tasharen.com/forum/index.php?topic=2171.0
Title: Re: Making UIButton Invisible
Post by: Divya Talluri on October 31, 2012, 12:02:29 AM
Hi JRoch,

ty for the helping me, but i have many buttons in the UI. Shall i attach the script to each button? Is that the correct way to do?
Title: Re: Making UIButton Invisible
Post by: JRoch on October 31, 2012, 01:49:08 AM
Either that, or write a script that walks the hierarchy on Awake() and stores handles to every object before you disable anything.  This means you'll want to have your panel start out of camera view (say -5000 X or something) and finish initialization with disabling any controls you want, perhaps even the parent panel, and then moving the panel via transform to the location you really want it.

There are a *lot* of different ways to approach object management.  You might think about creating a new test project, or a test scene, and then doing a bunch of experimentation before you spend a lot of time modifying your "live" project or scene.
Title: Re: Making UIButton Invisible
Post by: Divya Talluri on October 31, 2012, 01:54:04 AM
Better I will go with the normal approach, becoz I have no time to do experiments now.But I will definetly try the new one when i am free.
Thank you so much.it helped a lot and saved my time :)
Title: Re: Making UIButton Invisible
Post by: Divya Talluri on November 01, 2012, 01:25:09 AM
hey I have another question on this..How to make this duplicate UIButton object disable or enable? I tried like typecasting it to UiButton but  gives error convert `UnityEngine.GameObject' to `UIButton'.Help me with this :(
Title: Re: Making UIButton Invisible
Post by: Divya Talluri on November 01, 2012, 03:47:04 AM
And another question too, how to set all UIButton properties to the duplicate game object? I want to disable it when on clicked and set it default color when clicked on another button. Please give sample code how to do this, becoz when i am trying to set the disable color  and default color they are not working, it looks same when after clicking.
Title: Re: Making UIButton Invisible
Post by: ArenMook on November 01, 2012, 09:27:24 AM
A game object can't be cast to a component type. You need to use GetComponent on it. Consult Unity's documentation for more information.
Title: Re: Making UIButton Invisible
Post by: JRoch on November 01, 2012, 12:37:34 PM
Aren's advice is about as much as I can help as well.  It's clear you are a fairly novice programmer with Unity, so you need to start with basics and educate yourself so you understand the code you are working with.
Title: Re: Making UIButton Invisible
Post by: Divya Talluri on November 02, 2012, 05:05:55 AM
thank u guys for the help. I learned a lot from forums. Everything is working fine now except one issue.
The issue is, as spoke about the GlobalGameObjectTrackerClass, when i am loading from another scene to this scene(where i have used the tracker class), i am getting error "MyBtn Cannot add item to GlobalGameObjectTracker hashtable."
Can u guys please suggest what to do with this?
Title: Re: Making UIButton Invisible
Post by: PhilipC on November 02, 2012, 05:52:13 AM
What is the type of MyBtn? Is it a GameObject or a UIButton or...? because my guess is that you are trying to add a invalid type to the GlobalGameObjectTracker.

My other thought would be that the hastable already has MyBtn and its not liking the duplicate entry.
Title: Re: Making UIButton Invisible
Post by: Divya Talluri on November 02, 2012, 06:36:56 AM
MyBtn is of type UIButton, the instance is being destroyed while moving to another scene.
Title: Re: Making UIButton Invisible
Post by: PhilipC on November 02, 2012, 08:32:03 AM
Ok so my next question would be, if MyBtn is of type UIButton are you trying to add MyBtn.gameobject to the hashtable or just MyBtn. I dont think (from what i understand by the name of the type) GlobalGameObjectTracker will only take GameObjects not UIButtons.
Title: Re: Making UIButton Invisible
Post by: JRoch on November 02, 2012, 11:29:50 AM
Philip has it right.  If you look at the two functions that allow you to add and retrieve items from the tracker, they are always expected to be of type GameObject.

You can, of course, write your own classes that have a different expected type for the hashtable.  It would be inadvisable to try to implement a hashtable tracker that stores any type, which you then cast on retrieval... mostly because as a human you'd end up forgetting details and be forever mis-using return values and troubleshooting the resulting bugs.
Title: Re: Making UIButton Invisible
Post by: Divya Talluri on November 03, 2012, 03:39:58 AM
I am accessing like MyBtn.gameobject in the script.Everything is fine when I am in the same Scene, but when moved from this scene to another scene,comes back to this scene getting that error.
As when moved to another scene instances are destroyed(may be this is the reason).I have used DontDestroyOnLoad() method on this gameobject, still it is giving the same errors

1.MyBtn Cannot add item to GlobalGameObjectTracker hashtable.

2.MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
NGUITools.SetActive (UnityEngine.GameObject go, Boolean state) (at Assets/NGUI/Scripts/Internal/NGUITools.cs:540)