x


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.

more ▼

asked Nov 04 '11 at 12:02 PM

UnityNeewb gravatar image

UnityNeewb
1 2 2 2

Do you want the delay when it starts as well as when it stops, or just when it starts?

Nov 04 '11 at 03:13 PM syclamoth
(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

add this to your code:

// do something
yield WaitForSeconds(3.0); // wait for 3 seconds
// do something more...

Source

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));
        }
    }
}
more ▼

answered Nov 04 '11 at 12:21 PM

andresp gravatar image

andresp
183 18 21 25

Where about :P i have tried this method but i don't know where to put it :P

Nov 04 '11 at 02:21 PM UnityNeewb

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.

Nov 04 '11 at 03:02 PM andresp

Nope doesn't work

Nov 04 '11 at 03:12 PM benjimazza

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."

Nov 04 '11 at 03:53 PM andresp

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.)

Nov 04 '11 at 03:59 PM syclamoth
(comments are locked)
10|3000 characters needed characters left
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;
}

}
more ▼

answered Nov 04 '11 at 05:45 PM

cj_coimbra gravatar image

cj_coimbra
366 13 16 17

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 RMB it started counting and it goes on for ever and ever and ever Any ideas ??

Nov 04 '11 at 05:51 PM UnityNeewb

Try now, let´s see if it helps !

Nov 04 '11 at 05:58 PM cj_coimbra

This works great :) how would i change this code so that instead of the GUITexture it was a camera ??

Nov 04 '11 at 06:17 PM UnityNeewb

What do you mean? Switch to another camera?

Nov 04 '11 at 06:21 PM cj_coimbra

Yeh :) so basicly instead of the GUITexture been activated in 3 seconds the camera is activated in 3 seconds if that makes sense, so basicly instead of the GUITexture its the camera ??

Nov 04 '11 at 06:25 PM UnityNeewb
(comments are locked)
10|3000 characters needed characters left
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));
}
more ▼

answered Nov 04 '11 at 03:16 PM

syclamoth gravatar image

syclamoth
15k 7 15 80

i get one error Assets/Scripts/Texture.js(4,28): BCE0043: Unexpected token: onOff. Any Clue ??

Nov 04 '11 at 04:53 PM UnityNeewb
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3808
x558
x26

asked: Nov 04 '11 at 12:02 PM

Seen: 775 times

Last Updated: Nov 04 '11 at 06:25 PM