Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
-1
Question by maroonrs2 · Apr 19, 2012 at 09:42 AM · waitforsecondswaitexecute

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image maroonrs2 · Apr 19, 2012 at 02:45 PM 0
Share

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.

avatar image fafase · Apr 19, 2012 at 03:25 PM 4
Share

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.

avatar image
2

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 );
     }
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image maroonrs2 · Apr 19, 2012 at 02:42 PM 0
Share

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.

avatar image Bunny83 · Apr 19, 2012 at 03:01 PM 0
Share

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()?

avatar image
1

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;
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image maroonrs2 · Apr 19, 2012 at 02:33 PM 0
Share

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

avatar image maroonrs2 · Apr 19, 2012 at 02:39 PM 0
Share

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

avatar image
-3

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Apr 19, 2012 at 03:46 PM 1
Share

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

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges