x


Waiting then Executing

var bullet : Transform;

function Update(){
    waitNGO();
}

function waitandgo()
{
    clone = Instantiate (bullet, transform.position, transform.rotation);
    yield WaitForSeconds(2);
    Destroy(clone.GameObject);
}

function waitNGO()
{
    yield waitandgo();
}

Here is my code. What is does is sprays endless ammounts of bullets (alot per frame). I do not know how to fix it. I have been to countless places, and needless to say no one has answered it to which i can find the answer. Thanks for your help :)

more ▼

asked Apr 19 '12 at 09:42 AM

maroonrs2 gravatar image

maroonrs2
-55 9 12 13

(comments are locked)
10|3000 characters needed characters left

4 answers: sort voted first

2 ways either you use GetButtonDown and you need to press each time or use a timer:

first way

function Update()
{
    if(Input.GetButtonDown("Jump"))
    {
        clone = Instantiate (bullet, transform.position, transform.rotation);
        Destroy(clone.gameObject,2.0f);
    }
}

second way is shooting every 2 seconds as you keep the button down:

var timing:int;

function Update()
{
    timing = Time.time %2;
    if(Input.GetButton("Jump"))
    {
        if(timing)
        {
            clone = Instantiate (bullet, transform.position, transform.rotation);
            Destroy(clone.gameObject,2.0f);
        }
    }
}

By the way you don't need to yield and destroy. The second argument passed to the destroy function is a timer.

more ▼

answered Apr 19 '12 at 09:57 AM

fafase gravatar image

fafase
10.5k 9 15 42

This was what i was looking for. There is the fixed Destroy () command and the Time.time value would be sufficiant to use instead of overloaded functions on one script. YOU NEED TO FIX YOUR SPACES AND TABS! This is kinda illegible. As i see it, var timeing:int; is a function and function Update() is a if and if is a if and if is another if and then you have to look were the }'s end. I will show you a correct Version that makes sence, not to doubt you know a lot more than i do. It might just take me a shorter time to learn it.

Apr 19 '12 at 02:45 PM maroonrs2

Dude are you serious? You get help and complain? Know what, last time with you moronrs2. You already showed a bad side of you earlier today in another post, now you confirm. Your -16 credit says it all on your programming skills, don't bother showing me your results.

Apr 19 '12 at 03:25 PM fafase
(comments are locked)
10|3000 characters needed characters left

Sure you call the function every Update so it's called every frame. Your whole coroutine "mess" effectively does this:

var bullet : Transform;

function Update(){
    clone = Instantiate (bullet, transform.position, transform.rotation);
    Destroy(clone.GameObject, 2);
}

It seems you want to slow down the instantiate rate. You can use a single coroutine for that which means it has to be started only once. Something like that:

var bullet : Transform;
var spawnDelay = 2.0;         // a bullet every 2 sec
var maxBulletLifeTime = 10.0; // destroy the bullet after 10 sec.

function Start()
{
    SpamBullets(); // Start the coroutine
}

function SpamBullets()
{
    while(true)
    {
        clone = Instantiate( bullet, transform.position, transform.rotation );
        Destroy( clone.GameObject, maxBulletLifeTime );
        yield WaitForSeconds( spawnDelay );
    }
}
more ▼

answered Apr 19 '12 at 10:32 AM

Bunny83 gravatar image

Bunny83
45.5k 11 49 207

Nope sorry, it starts one instance. Then it will repeat the clone process too many times in one go. Then it will wait. so like 20 instances was recorded in 2 seconds. It was confusing cause it had like a short trail.

Apr 19 '12 at 02:42 PM maroonrs2

That doesn't make much sense. Are you sure you have only one instance of this script on your "cannon" object and are you sure you called the coroutine in Start() and not in Update()?

Apr 19 '12 at 03:01 PM Bunny83
(comments are locked)
10|3000 characters needed characters left

You put an yield inside waitNGO function, but that's not sufficient: function Update continues to operate every frame. As a result, you have a bunch of calling to the wait NGO function: each of them will wait using the yield, but all of them will be already "queued" for the execution, and the "wait" is nullified. You can solve introducing a variable (lock) that controls when waitNGo has finished or not its execution.

var bullet : Transform;
var lock = false;

function Update(){
   if (!lock)
      waitandgo();
}

function waitandgo()
{
    lock = true;
    clone = Instantiate (bullet, transform.position, transform.rotation);
    yield WaitForSeconds(2);
    Destroy(clone.GameObject);
    lock = false;
}
more ▼

answered Apr 19 '12 at 09:50 AM

BiG gravatar image

BiG
4.7k 4 13 49

So what you just said is add a debouncer. Gotcha.

Apr 19 '12 at 02:33 PM maroonrs2

Good work but im going to see what other people posted.

Apr 19 '12 at 02:39 PM maroonrs2
(comments are locked)
10|3000 characters needed characters left
function Update(){
    if(Input.GetButtonDown("Jump")
    {
           clone = Instantiate (bullet, transform.position, transform.rotation);
           Destroy(clone.gameObject,2.0f);
    }
}

var timing:int;

function Update()
{
    timing = Time.time %2;
        if(Input.GetButton("Jump")){
            if(timing){
                clone = Instantiate (bullet, transform.position, transform.rotation);
                Destroy(clone.gameObject,2.0f);}
            }
        }
   }

And i also see that you forgot a } in the process

more ▼

answered Apr 19 '12 at 02:59 PM

maroonrs2 gravatar image

maroonrs2
-55 9 12 13

First he didn't forget the "}"... it's still behind the Destroy call so you have one more now. Also you didn't align your code that much better.

Second why do you post his solution as additional answer?

Apr 19 '12 at 03:46 PM Bunny83
(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:

x5095
x170
x59
x17

asked: Apr 19 '12 at 09:42 AM

Seen: 584 times

Last Updated: Apr 19 '12 at 03:50 PM