- Home /
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 :)
Answer by fafase · Apr 19, 2012 at 09:57 AM
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.
This was what i was looking for. There is the fixed Destroy () command and the Time.time value would be sufficiant to use ins$$anonymous$$d 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.
Dude are you serious? You get help and complain? $$anonymous$$now 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 program$$anonymous$$g skills, don't bother showing me your results.
Answer by Bunny83 · Apr 19, 2012 at 10:32 AM
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 );
}
}
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.
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()?
Answer by BiG · Apr 19, 2012 at 09:50 AM
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;
}
Good work but im going to see what other people posted.
Answer by maroonrs2 · Apr 19, 2012 at 02:59 PM
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
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?
Your answer
Follow this Question
Related Questions
Csharp: how to make script wait for x seconds 3 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Activate Animation => Wait => Hide Gameobject 2 Answers
WaitForSeconds Audio Help 2 Answers
Add wait time to enemy's attack 2 Answers