Author Topic: InvokeRepeating not working when trigger from OnDragEnd()  (Read 3817 times)

senerdude

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 4
    • View Profile
InvokeRepeating not working when trigger from OnDragEnd()
« on: July 16, 2014, 07:06:48 AM »
Hi I have some problems with OnDragStart() OnDragEnd() events.. UIDragDropItem attached to my object and "Clone On Drag" is selected.

  1.     void OnDragStart()
  2.     {
  3.             // Change cloned drag object sprite to spell target indicator
  4.             targetIndicator = UICamera.currentTouch.dragged.GetComponent<UISprite>();
  5.             targetIndicator.spriteName = "PitsOfDefence";
  6.             targetIndicator.width = 300;
  7.             targetIndicator.height = 300;
  8.  
  9.             // Slow Time When Object Draging
  10.             Time.timeScale = 0.25f;
  11.     }
  12.  
  13. void OnDragEnd()
  14. {
  15.    // Problem One : I need to disable dragging during the cooldown. but how? I tryed to disable BoxCollider, UIDragDropItem not working.
  16.  
  17.  // gameObject.GetComponentInParent<BoxCollider>().enabled = false;
  18. // gameObject.GetComponentInParent<UIDragDropItem>().enabled = false;
  19.  
  20.   // Instantiating Spell Prefab to scene
  21.   Instantiate(spellPrefab, Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 8.5f)), Quaternion.identity);
  22.  
  23.  // Normalize Time
  24.  Time.timeScale = 1f;
  25.  
  26.  
  27. // Start Cooldown Indicator
  28. cooldownSprite.fillAmount = 1; // Fill Cooldown
  29. progressPerSecond = ((100f / (PitsofDefenceSpell.DamageDuration + 1f)) / 1000f);
  30. InvokeRepeating("ShowCoolDown", 0, 0.1f); // ( *********** Problem Two :  Its not working when fire InvokeRepeating. what is wrong? ************* )
  31.  
  32. }
  33.  
  34.     void ShowCoolDown()
  35.     {
  36.         if (cooldownSprite.fillAmount <= 0)
  37.         {
  38.             CancelInvoke();
  39.         }
  40.         cooldownSprite.fillAmount -= progressPerSecond;
  41.     }
  42.  

Thank you

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: InvokeRepeating not working when trigger from OnDragEnd()
« Reply #1 on: July 17, 2014, 02:02:46 AM »
InvokeRepeating is not NGUI's functionality -- it's Unity's, so you need to check Unity's documentation. Note that all coroutines won't run while the component is disabled. To disable dragging of something, disabling its collider would work, but it has to be done after OnDragEnd, because the object's collider is already disabled while it's being dragged.

senerdude

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 4
    • View Profile
Re: InvokeRepeating not working when trigger from OnDragEnd()
« Reply #2 on: July 17, 2014, 03:49:23 AM »
InvokeRepeating is not NGUI's functionality -- it's Unity's, so you need to check Unity's documentation. Note that all coroutines won't run while the component is disabled. To disable dragging of something, disabling its collider would work, but it has to be done after OnDragEnd, because the object's collider is already disabled while it's being dragged.

Thank you, I checked documentation I cound find anything about why its not working. but InvokeRepeating working everywhere except when called in your functions.. its not show error or log, like I didnt call it. is it coud be related to NGUI? 

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: InvokeRepeating not working when trigger from OnDragEnd()
« Reply #3 on: July 18, 2014, 04:27:21 AM »
I did a test:

1. New scene.
2. Created a simple sprite (ALT+SHIFT+S), changed it to be Filled type.
3. Dragged in a Control - Simple Button, attached this script to it:
  1. using UnityEngine;
  2.  
  3. public class Test : MonoBehaviour
  4. {
  5.         public UISprite sprite;
  6.  
  7.         void OnClick ()
  8.         {
  9.                 InvokeRepeating("TestMe", 0.1f, 0.1f);
  10.         }
  11.  
  12.         void TestMe ()
  13.         {
  14.                 sprite.fillAmount -= 0.1f;
  15.                 if (sprite.fillAmount == 0f) CancelInvoke();
  16.         }
  17. }
4. Referenced the sprite from #2 on the script.
5. Hit Play, click the button. The sprite gets sliced up at 0.1 second intervals over a period of 1 second until it's gone.

senerdude

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 4
    • View Profile
Re: InvokeRepeating not working when trigger from OnDragEnd()
« Reply #4 on: July 18, 2014, 07:02:46 AM »
Thank you for your time ArenMook.

InvokeRepeating workng fine in OnClick().

In my game there is two kind of button. first one is clickable. second one is draggable.
Clickable ones working just fine like your sample.
but if I call InvokeRepeating from OnDragEnd() or OnDragStart() its just not working with no error.
 
Can you try to call InvokeRepeating from OnDragEnd()?

Thank you

Edit : UIDragDropItem attached to object and "Clone On Drag" is checked.


ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: InvokeRepeating not working when trigger from OnDragEnd()
« Reply #5 on: July 18, 2014, 07:57:22 AM »
I just modified the test script, renaming OnClick to OnDragEnd. Now I press on the button, move the mouse away and release it. As soon as I do, the InvokeRepeating kicks in and the sprite animates same as before with OnClick.

I suggest looking into your own logic there, and adding OnDestroy() or OnDisable() to your script to see when the object gets disabled or destroyed. My guess is that the lifetime of your object is not what you expect it to be.

senerdude

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 4
    • View Profile
Re: InvokeRepeating not working when trigger from OnDragEnd()
« Reply #6 on: July 21, 2014, 03:31:02 AM »
Thank you, ArenMook.

I understand the problem now. I'm adding "Drag Drop Item" script and check "Clone On Drag"? so script working on cloned object. When drag end destorying cloned object.. You right my logic is wrong I assumed scritp working on original object. I need to find another way to do that.

Thanks for your time.