Search Unity

need help for loop and reset GUI

Discussion in 'Scripting' started by arcticmonkey, Jul 24, 2014.

  1. arcticmonkey

    arcticmonkey

    Joined:
    Oct 3, 2013
    Posts:
    4
    hi ...
    iam trying to create something like a slideshow with GUI, OnTriggerEnter and exit but i dont get it how to loop the textures and reset the GUI to texture 1 after exit the trigger (or re enter)

    any ideas?

    thanks in advance =)

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. private var guiShow : boolean = false;
    4.  
    5. var texture1 : Texture;
    6. var texture2 : Texture;
    7. var texture3 : Texture;
    8. var texture4 : Texture;
    9.  
    10.  
    11. function OnTriggerEnter (Col : Collider)
    12. {
    13.     if(Col.tag == "Player")
    14.     {
    15.         guiShow = true;
    16.     }
    17. }
    18.  
    19. function OnTriggerExit (Col : Collider)
    20. {
    21.     if(Col.tag == "Player")
    22.     {
    23.         guiShow = false;
    24.     }
    25.  
    26. }
    27.  
    28. function OnGUI()
    29. {
    30.     if(guiShow == true)
    31.     {
    32.         GUI.DrawTexture(Rect(Screen.width / 5.5, Screen.height / 10, 1224, 818), texture1);
    33.    
    34.     wait();
    35.                
    36.     }
    37.    
    38. }
    39.  
    40. function wait()
    41. {
    42.     yield WaitForSeconds(1);
    43.  
    44.         texture1=texture2;
    45.  
    46.     yield WaitForSeconds(1);
    47.  
    48.         texture2=texture3;
    49.        
    50.     yield WaitForSeconds(1);
    51.  
    52.         texture3=texture4;
    53.        
    54. }
    55.  
     
  2. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    Use an array and a counter. OnTriggerEnter --> counter++

    Use that then to loop through the textures in OnGUI
     
  3. arcticmonkey

    arcticmonkey

    Joined:
    Oct 3, 2013
    Posts:
    4
    thanks for advice but iam still struggling with this ...
    problem is, i dont know why it jumps between i = 0,1,2 and never goes up to i = 3
    and the yield function will not work doesnt matter where i place it ... now its like a strobscope ^^

    iam new to this so maybe theres a BIG fail in the code

    thanks again!

    Code (JavaScript):
    1. pragma strict
    2.  
    3. private     var guiShow : boolean = false;
    4.  
    5.             var texture : Texture[];
    6. private        var i : int;
    7.              
    8.  
    9. function OnTriggerEnter (Col : Collider)
    10. {
    11.     if(Col.tag == "Player")
    12.  
    13.     {
    14.         guiShow = true;
    15.      
    16.     }
    17.     for (var i : int =0; i <= 3; i++)
    18.      
    19.         {
    20.      
    21.         }
    22. }
    23.  
    24. function OnTriggerExit (Col : Collider)
    25. {
    26.     if(Col.tag == "Player")
    27.     {
    28.         guiShow = false;
    29.  
    30.     }
    31.  
    32. }
    33.  
    34. function OnGUI()
    35. {
    36.     if(guiShow == true)
    37.     {
    38.         GUI.DrawTexture(Rect(Screen.width / 5.5, Screen.height / 10, 1224, 818), texture[i++]);
    39.         loop();  
    40.     }
    41.  
    42. }
    43.  
    44. function loop()
    45. {
    46. if(i==3)
    47.     {
    48.     i=0;
    49.     return;
    50.     }
    51. }
     
  4. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    You are calling loop every frame. That does not work. You should not be calling such functions in OnGUI.

    Also,, don't use i++ in DrawTexture. How fast do you want to loop through the textures?
     
  5. arcticmonkey

    arcticmonkey

    Joined:
    Oct 3, 2013
    Posts:
    4
    every 2 secondes 1 texture ..
     
  6. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    Then you need to make a timer in update.
     
  7. arcticmonkey

    arcticmonkey

    Joined:
    Oct 3, 2013
    Posts:
    4
    thanks! got it ... :)