Search Unity

trouble using InvokeRepeating to flash objects visibilty

Discussion in 'Scripting' started by vmp, Jan 14, 2011.

  1. vmp

    vmp

    Joined:
    Mar 13, 2006
    Posts:
    23
    in the following script the objects visibility isnt toggled and it seems like cancelInvoke is cancelling in the first run through instead of subtracting from the counter and toggling the visibility then canceling when the counter has reached 0.
    if i remove the CancelInvoke the objects visibility toggles on and off like i would expect

    i think i am using it incorrectly but for the life of me i cannot figure whats up with the script
    any ideas? on how to get this to work

    i know i can do it otherways but i would like to get the invoke repeating to work, so that i understand how it should be used etc.
    thanks

    Code (csharp):
    1. var counter:int=4;
    2. private var hidden:boolean=false;
    3. function Update()
    4. {
    5.     if(Input.GetButton("Jump"))
    6.         {
    7.             Input.ResetInputAxes();
    8.             counter = 4;
    9.             InvokeRepeating("toggleVisibilty", 0, .3);
    10.             Debug.Log("Finished flashing");
    11.         }
    12. }
    13.  
    14. function toggleVisibilty()
    15. {
    16.      GameObject.Find("Roach_Sprite").GetComponent(MeshRenderer).enabled = hidden; // stop rendering the player.
    17.  
    18.      hidden = !hidden;
    19.      Debug.Log(counter);
    20.      Debug.Log(hidden);
    21.      counter --;
    22.      if (counter == 0)
    23.         CancelInvoke("toggleVisibilty");
    24. }
     
    Last edited: Jan 14, 2011
  2. vmp

    vmp

    Joined:
    Mar 13, 2006
    Posts:
    23
    bump
     
  3. vmp

    vmp

    Joined:
    Mar 13, 2006
    Posts:
    23
    bump..

    any suggestions as too where i might be going wrong with the use of invokeRepeating and cancelInvoke?
     
  4. giyomu

    giyomu

    Joined:
    Oct 6, 2008
    Posts:
    1,094
    I dont think having invoke call GameObject.Find is a very good idea performance wise ^^.

    anyway I will prefer have this script on your object that need to toggle it visibility then just enable the script

    so something like that maybe (sorry C# )

    Code (csharp):
    1.  
    2. public float toggleTime = 0.3f;
    3. public int counter = 4;
    4.  
    5. //private
    6. int storedCounter;
    7.  
    8. void Awake()
    9. {
    10.    //I assume the script may be off when you start.
    11.    enabled = false;
    12.  
    13.    storedCounter = counter;
    14. }
    15.  
    16. void OnEnable()
    17. {
    18.      counter = storedCounter;
    19.      InvokeRepeating("ToggleVisibility",0.01f, toggleTime);
    20. }
    21.  
    22. public void ToggleVisibility()
    23. {
    24.     //check that you have renderer component on this object (just defensive programming ,
    25.     //you can get rid of that if later )
    26.     if(!renderer)
    27.     {
    28.        enabled = false;
    29.        return;
    30.     }
    31.     //Do your toggle---------
    32.     else if(counter > 0)
    33.     {
    34.         renderer.enabled = !renderer.enabled;
    35.         counter --;
    36.     }
    37.     else
    38.         enabled = false;
    39. }
    40.  
    41. void OnDisable()
    42. {
    43.    CancelInvoke("ToggleVisibility");
    44. }
    45.  
    46.  
    Doing so you just fire you script form somewhere when you need

    hope that help ;)

    oh by the way you could then just fire this script when your button is pressed then
     
    Last edited: Jan 16, 2011
  5. giyomu

    giyomu

    Joined:
    Oct 6, 2008
    Posts:
    1,094
    hmm anyway if what you wanted to do is hide the player for X time once then show it again in that case use coroutine and yield instead.
    My anser was based on a flash toggle like old skool game when you get back in game and stay invincible for a little bit of time..
     
  6. vmp

    vmp

    Joined:
    Mar 13, 2006
    Posts:
    23
    thanks for the help.. i will give your code a try and see how i go
     
  7. vmp

    vmp

    Joined:
    Mar 13, 2006
    Posts:
    23
    Thanks giyomu for you help, works like a charm
     
  8. vmp

    vmp

    Joined:
    Mar 13, 2006
    Posts:
    23
    manage to work out what was wrong with my script i had

    InvokeRepeating("toggleVisibilty", 0, .3);

    but it needed to be InvokeRepeating("toggleVisibilty", 0f, .3f);

    does the f mean its a float?
     
  9. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    If you don't add the "f" to the number in C#, then the compiler assumes you want the number to be a double rather than a float. Unity uses floats for most things in the interests of performance.
     
  10. giyomu

    giyomu

    Joined:
    Oct 6, 2008
    Posts:
    1,094
    glad that helped you

    by the way i realize ( if you didn't before me ) that you better to move the check for renderer in OnEnable(), rather to check for it every repeat XD...my bad ^^

    well just a note tho :)