- Home /
 
disappear script that last a certain amount of time
i have created a guitext that i have programmed to turn off when i press one of two different buttons. they both turn it off for as long as i hold that button, so if i dont it will just flash off then on again. My problem is that for one of the two key commands i need a timer the tell it how long to shut off for. I believe i am very close to the final script i just am not sure what i need to do. Im pretty sure i need to set another var or two but not entirely. Any and All help is appreciated!
Normal script:
 var go : GameObject;
 
 function Update () {
     
     go.SetActive(!Input.GetKey(KeyCode.LeftShift));
     go.SetActive(!Input.GetKey(KeyCode.R));
 }
 
               My FAIL edit:
 var go : GameObject;
 
 function Update () {
     
     go.SetActive(!Input.GetKey(KeyCode.LeftShift));
     
     if(Input.GetKey(KeyCode.R)){
     Reload();
     }
 }
 
 function Reload () {
         go.SetActive(!Input.GetKey(KeyCode.R)) = true;
         yield WaitForSeconds(2);
         go.SetActive(Input.GetKey(KeyCode.R)) = false;
 }
 
              Answer by wolfadex · Jul 25, 2013 at 01:57 AM
The problem was that when you released 'R', 'LeftShift' was not pressed so the guiText was set to active. This should work better:
 #pragma strict
 var hiddenTime = 2;
 var releaseTime = 0;
 
 function Start () {
 
 }
 
 function Update () {
 
     if (Input.GetKey(KeyCode.LeftShift)) {
         gameObject.guiText.enabled = false;
         releaseTime = -hiddenTime;
     }
     
     if (Input.GetKey(KeyCode.R)) {
         gameObject.guiText.enabled = false;
         releaseTime = Time.time;
     }
     
     if ( Time.time - releaseTime >= hiddenTime ) {
         gameObject.guiText.enabled = true;
     
 
              makes sense, thank you ill be trying this when i get a chance
Answer by Grim_Darknight · Jul 25, 2013 at 02:24 AM
You can set a timer using time.deltaTime * 1.
 function Update()
 {
   go.SetActive(!Input.GetKey(KeyCode.LeftShift));
     
   if(Input.GetKey(KeyCode.R))
   {
     timer = 0;  // this will set the timer variable to 0 as long as R is pressed
   }
 
   if (timer < visibilityTimer)
   {
     go.SetActive(false);
     timer += (time.deltaTime * 1); // if the timer variable is less than visibility timer variable it will increase 1 unit per second.
   }
   else
   {
     go.SetActive(true);
   }
 }
 
               For this to work you will need to set up 2 variables:
 private var timer : float;
 var visibilityTimer : int;
 
               the timer variable must be a float or it won't work, the visability timer variable can be float or int; also if you set visibility timer to public you can customize the timing from the inspector.
your script makes sense, however when i go to press the other button, it does not dissappear, and wehen the character spawns the gui turns off for 2 seconds then on. i can solve the shift key button with using my old script seperatley, but the spawn error is what gets me
actually no it isn't, either way i cant get the shift key to make the gui disappear
Your answer