- Home /
Delaying GUITexture!!!
Hey :) .. How would i put a delay in this.. to go in to more detail i want a delay just before my GuiTexture activates This is the code it all works but i want a delay off like 3 seconds befor it actully activates anybody know how to do this??...
var Aim : boolean = false;
var SnipeTexture : GUITexture;
function Update ()
{
if(Input.GetMouseButton(1))
{
Aim = true;
}
if(Input.GetMouseButtonUp(1))
{
Aim = false;
}
if(SnipeTexture == null)
return;
if(Aim)
{
SnipeTexture.enabled = true;
}
if(!Aim)
{
SnipeTexture.enabled = false;
}
}
Thanks.
Do you want the delay when it starts as well as when it stops, or just when it starts?
Answer by andresp · Nov 04, 2011 at 12:21 PM
add this to your code:
// do something
yield WaitForSeconds(3.0); // wait for 3 seconds
// do something more...
EDIT - C# Example:
using UnityEngine; using System.Collections;
public class test : MonoBehaviour {
bool calledDelay = false;
bool doneDelay = false;
IEnumerator Wait(float sec)
{
yield return new WaitForSeconds(sec);
Debug.Log("3 seconds later");
doneDelay = true;
}
void Update () {
if (doneDelay)
Debug.Log("Do something");
else if (!calledDelay)
{
calledDelay = true;
StartCoroutine(Wait(3f));
}
}
}
Where about :P i have tried this method but i don't know where to put it :P
I think you would want it at the beginning of Update() or just before the activation of your GUI script in some other script that activates the GUI object/component.
it works. you can't use it inside Update but you can you use StartCoroutine to start a method that can use yield (just as you can see in the link that I posted).
"Note that you can't use yield from within Update or FixedUpdate, but you can use StartCoroutine to start a function that can."
You can't use yield from within Update, because that would cause the game to hang for a frame! (which it can't because it would be waiting for the update loop to finish, which itself would be waiting for the update loop to finish.)
Answer by syclamoth · Nov 04, 2011 at 03:16 PM
function WaitAndSnipe(bool onOff)
{
aim = onOff;
yield WaitForSeconds(3.0);
if(aim == onOff)
{
SnipeTexture.enabled = onOff;
}
}
Then, call it from Update inside of
if(Input.GetMouseButtonDown(1))
{
StartCoroutine(WaitAndSnipe(true));
}
if(Input.GetMouseButtonUp(1))
{
StartCoroutine(WaitAndSnipe(false));
}
i get one error Assets/Scripts/Texture.js(4,28): BCE0043: Unexpected token: onOff. Any Clue ??
Answer by cj_coimbra · Nov 04, 2011 at 05:45 PM
var Aim : boolean = false;
var repeating : boolean = false;
var lastActivationTime;
var SnipeTexture : GUITexture;
function Update ()
{
if(Input.GetMouseButton(1)) { Aim = true; if (repeating == false) { lastActivationTime = Time.time; repeating = true; } }
if(Input.GetMouseButtonUp(1))
{ Aim = false; repeating = false; }
if(SnipeTexture == null)
return;
if(Aim && (Time.time - lastActivationTime > 3.0)) { SnipeTexture.enabled = true; }
if(!Aim) { SnipeTexture.enabled = false; }
}
Hey Thanks for the comment, but there's a problem with this code all this code does is make a counter so that when i hold the R$$anonymous$$B it started counting and it goes on for ever and ever and ever Any ideas ??
This works great :) how would i change this code so that ins$$anonymous$$d of the GUITexture it was a camera ??
Yeh :) so basicly ins$$anonymous$$d of the GUITexture been activated in 3 seconds the camera is activated in 3 seconds if that makes sense, so basicly ins$$anonymous$$d of the GUITexture its the camera ??
Your answer
Follow this Question
Related Questions
Exiting witha GUI Button 1 Answer
activating a box using a GUI button 1 Answer
Collider Animation 1 Answer
'Mission Complete/Game Over' screen once objective is complete. 1 Answer
GUI Scroll View 1 Answer