Author Topic: Enable/disable Panels  (Read 6985 times)

Lightning99

  • Guest
Enable/disable Panels
« on: September 04, 2013, 06:42:37 PM »
I have a UI ROOT(2D). My anchor has two panels attached to it(Panel1, Panel2). When the player completes the level I want Panel 1 to disappear and then Panel 2 appears, possibly with an animation but i will worry about that later. I have been looking for how to do this for a few hours and have extensively studied the main menu example but can't seem to get anything to work. Thank you!

Yukichu

  • Full Member
  • ***
  • Thank You
  • -Given: 3
  • -Receive: 8
  • Posts: 101
    • View Profile
Re: Enable/disable Panels
« Reply #1 on: September 04, 2013, 07:56:46 PM »
Well... can't you disable/enable the UIPanel?

get the IUPanel object, and it's like muiPanel.enabled = false

Other option is enable/disable the panel game object

muiPanel.gameObject.setActive(false)

I believe the second one is more intensive, as I've been told enable/disable rebuilds the colliders (but seems no one knows, or no one I talked to, for sure)

Lightning99

  • Guest
Re: Enable/disable Panels
« Reply #2 on: September 04, 2013, 08:05:38 PM »
I have tried that. I have used
 
  1. var panel1 : GameObject = GameObject.Find("Panel1");
  2. panel1.SetActive(true);
  3.  
  4. // and things like
  5. GameObject.Find("panel2").GetComponent("UIPanel").enabled = false
  6.  

but it never works right and i always get some error, i have tried many variations of this

ENAY

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 248
    • View Profile
Re: Enable/disable Panels
« Reply #3 on: September 04, 2013, 09:30:47 PM »
How about?

panel1.gameObject.SetActiveRecursively(false);

Lightning99

  • Guest
Re: Enable/disable Panels
« Reply #4 on: September 04, 2013, 10:02:53 PM »
I tried that and just SetActive. Both times i get an error Destroying GameObjects Immediately is not permitted during physics/trigger/contact or animate event callbacks. You must use Destroy Instead.

LightSky

  • Jr. Member
  • **
  • Thank You
  • -Given: 3
  • -Receive: 2
  • Posts: 56
    • View Profile
Re: Enable/disable Panels
« Reply #5 on: September 05, 2013, 01:07:46 AM »
If you need to Enable/Disable a GameObject use NGUITools.SetActive.
For the animations just have the animations be played OnEnable and have it toggle back and forth.  This will give it the fly in and out effect or something similar if that is what you want.

ENAY

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 248
    • View Profile
Re: Enable/disable Panels
« Reply #6 on: September 05, 2013, 03:05:42 AM »
Where are you destroying objects? Seems like you have a separate issue to fix first, or wait until what you're trying to delete has executed and started before you start touching the panels.

Lightning99

  • Guest
Re: Enable/disable Panels
« Reply #7 on: September 05, 2013, 02:54:01 PM »
I am using unity script and already made a plugin folder and put all the stuff into it that i needed to so that is not the problem. I am not destroying anything the code I am using is
  1. function OnCollisionStay(col : Collision) {  
  2.                
  3.                 if(col.gameObject.tag == "End"){
  4.                         grandController = false;                               
  5.                         var panel2 : GameObject = GameObject.Find("Panel2");
  6.                         NGUITools.SetActive(panel2,false);
  7.         }
  8. }
  9.  
it successfully finds panel2 but then i get an error it says"Destroying GameObjects immediately is not permitted during physics trigger/contact or animation event callbacks. You must use Destroy instead.
UnityEngine.Object:DestroyImmediate(Object)
NGUITools:DestroyImmediate(Object) (at Assets/Plugins/NGUI/Internal/NGUITools.cs:514)
UIPanel:OnDisable() (at Assets/Plugins/NGUI/UI/UIPanel.cs:700)
UnityEngine.GameObject:SetActive(Boolean)
NGUITools:SetActiveSelf(GameObject, Boolean) (at Assets/Plugins/NGUI/Internal/NGUITools.cs:669)
NGUITools:Deactivate(Transform) (at Assets/Plugins/NGUI/Internal/NGUITools.cs:601)
NGUITools:SetActive(GameObject, Boolean) (at Assets/Plugins/NGUI/Internal/NGUITools.cs:617)"
I have no clue why something in NGUI Tools called Destroy immediate is involved with what I am doing but unity doesn't seem to like it

Lightning99

  • Guest
Re: Enable/disable Panels
« Reply #8 on: September 05, 2013, 03:03:09 PM »
Also the game can Run I do not get the error until that code is called at the end of the level. And what is really weird is that the code I have does successfully deactivate panel2 but the contents of it(a label currently for testing) do not turn off with it and remain there. And then after that when I mess around with it in the editor it acts very weird. If i try typing new stuff into the label it types it below the current label, and there is no way for me to do anything to the original label unless i exit and re enter Unity.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Enable/disable Panels
« Reply #9 on: September 05, 2013, 08:22:05 PM »
You can't enable/disable things from within a physics callback, as Unity tells you.

You need to do it via a Coroutine instead. In your physics callback do StartCoroutine, and inside that coroutine actually enable/disable whatever you need.

Oh, and don't enable/disable the panel component. SetActive its game object instead.

P.S. FYI, Starlink UI kit has a very robust window transition framework, complete with history and "go back" features.