Author Topic: TweenAlpha doesn't tween all children / Nullreference  (Read 2383 times)

BehindTheStone

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 135
    • View Profile
TweenAlpha doesn't tween all children / Nullreference
« on: January 13, 2014, 09:54:03 AM »
Hey there.

So do I miss something or why does TweenAlpha not tween all of my GameObjects Children?

Here's my setup.

This is my Hierarchy:



So my "01INVENTORYMENU" is an empty GameObject.
The Objects: "ITEMGRID", "Sticker", "Textbox" are simple Sprites:



The Objects "SLOT1" to "SLOT4" are empty UI-Widgets. "Itemlabel" is a simple label:



All the Sprite- and Label-Components are disabled by default.

So, what do I want to achieve:

I have an InventoryButton, when clicked this button and other buttons are disabled for a moment, an Animation is played and other stuff happens. This other stuff includes setting the Alpha-Value of the Children of "01INVENTORYMENU" to 0.0f and enabling the Sprite/Label-Components. This is how I do it:

  1.  
  2. public GameObject pageFlipper;
  3.         public tk2dSpriteAnimator flipperAnimator;
  4.  
  5.         public GameObject inventoryMenu;
  6.  
  7.         private UISprite[] spriteComp;
  8.         private UILabel[] labelComp;
  9.  
  10. // Start-Function just adds the EventDelegates...
  11.  
  12. IEnumerator ShowInventory()
  13.         {
  14.                 inventoryBtn.isEnabled = false;
  15.                 questmenuyBtn.isEnabled = false;
  16.                 diarymenuBtn.isEnabled = false;
  17.                 pageFlipper.renderer.enabled = true;
  18.  
  19.                 if(!flipperAnimator.IsPlaying("PageForward"))
  20.                 {
  21.                         flipperAnimator.Play("PageForward");
  22.                         flipperAnimator.AnimationCompleted = HitCompleteDelegate;
  23.                 }
  24.  
  25.                 foreach(UISprite s in spriteComp)
  26.                 {
  27.                         s.alpha = 0.0f;
  28.                         s.enabled = true;
  29.                 }
  30.                 foreach(UILabel l in labelComp)
  31.                 {
  32.                         l.alpha = 0.0f;
  33.                         l.enabled = true;
  34.                 }
  35.                 foreach(Transform child in inventoryMenu.transform)
  36.                 {
  37.                         TweenAlpha.Begin(child.gameObject, 1.0f, 1.0f);
  38.                 }
  39.  
  40.                 yield return new WaitForSeconds(2.0f);
  41.  
  42.                 inventoryBtn.isEnabled = true;
  43.                 questmenuyBtn.isEnabled = true;
  44.                 diarymenuBtn.isEnabled = true;
  45.  
  46.         }

I dragged and drop "01INVENTORYMENU" into the "inventoryMenu"-variable from the script.

And it works, but only the GameObject "ITEMGRID" is faded in. The UISprite/-Label-Components are enabled and the Alpha-Value is set to 0.0f, but they won't fade in. Just for testing I dragged "Sticker" in there, and only the child "Textbox" did fade in, but not the "Itemlabel".

Is there something wrong with my code?






« Last Edit: January 13, 2014, 01:45:45 PM by BehindTheStone »

BehindTheStone

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 135
    • View Profile
Re: TweenAlpha doesn't tween all children / Nullreference
« Reply #1 on: January 13, 2014, 01:46:07 PM »
Allright, I got the error, of course only the next child will be faded in since I only check the next child. So I managed to get that work, but now I got another problem.

I editet my script and added another Function, which tweens another Menu:

  1. IEnumerator ShowQuests()
  2.         {
  3.                 spriteComp = questMenu.GetComponentsInChildren<UISprite>();
  4.                 labelComp = questMenu.GetComponentsInChildren<UILabel>();
  5.                 children = questMenu.GetComponentsInChildren<Transform>();
  6.  
  7.  
  8.                 inventoryBtn.isEnabled = false;
  9.                 questsBtn.isEnabled = false;
  10.                 diaryBtn.isEnabled = false;
  11.                 pageFlipper.renderer.enabled = true;
  12.                
  13.                 if(!pageFlipper.IsPlaying("PageForward"))
  14.                 {
  15.                         pageFlipper.Play("PageForward");
  16.                         pageFlipper.AnimationCompleted = HitCompleteDelegate;
  17.                 }
  18.  
  19.                 foreach(UISprite s in spriteComp)
  20.                 {
  21.                         s.alpha = 0.0f;
  22.                         s.enabled = true;
  23.                 }
  24.                 foreach(UILabel l in labelComp)
  25.                 {
  26.                         l.alpha = 0.0f;
  27.                         l.enabled = true;
  28.                 }
  29.                 foreach(Transform c in children)
  30.                 {
  31.                         TweenAlpha twA = TweenAlpha.Begin(c.gameObject, 1.0f, 1.0f);
  32.                         EventDelegate.Add(twA.onFinished, ShowQuestmenu_Finished);
  33.                 }
  34.  
  35.  
  36.                 yield return new WaitForSeconds(1.0f);
  37.  
  38.  
  39.  
  40.         }

So, when the menu is up and I click my InventoryButton, the InventoryMenu fades in.
When I click the Quest-Button the QuestMenu fades in but it also throws this error a couple of times:

Quote
NullReferenceException: Object reference not set to an instance of an object
TweenAlpha.set_value (Single value) (at Assets/NGUI/Scripts/Tweening/TweenAlpha.cs:45)
TweenAlpha.OnUpdate (Single factor, Boolean isFinished) (at Assets/NGUI/Scripts/Tweening/TweenAlpha.cs:51)
UITweener.Sample (Single factor, Boolean isFinished) (at Assets/NGUI/Scripts/Tweening/UITweener.cs:282)
UITweener.Update () (at Assets/NGUI/Scripts/Tweening/UITweener.cs:208)

I don't get this. I use the exact same code on this, just other references/names. When the Inventory is tweened no Errors, when the Questmenu is tweened, errors :/




Edit:


Alright, I'll never post again before I have not made some research. After looking up this topic here: http://www.tasharen.com/forum/index.php?topic=7235.msg34119#msg34119
Stuff is clearer now.
Just added an if-Statement this line of code:

Quote
if(c.GetComponent<UILabel>() != null)
         {
            TweenAlpha twB = TweenAlpha.Begin(c.gameObject, 1.0f, 1.0f);
            EventDelegate.Add(twB.onFinished, ShowQuestmenu_Finished);
         }

And the errors are gone, of course.
« Last Edit: January 13, 2014, 02:22:51 PM by BehindTheStone »