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
0
Question by robinnicole · Dec 09, 2012 at 06:48 AM · javascriptgameobjectdestroyco-routine

Co Routine

Hello community, I don't see an obvious answer to my question on this site, so i am sending out. I have tried to create a co-routine to do the following:

--Instantiate a prefab of 'bluefruit" every 3 seconds.. --destroy the bluefruit after 10 seconds if they are not 'claimed' by the player ` --Here is the code. This script is assigned ot an Empty GameObject in the scene. The point is that the user will have 3 seconds to collect the blue fruit . Please advise.

 pragma strict
 
 var bluefruit : GameObject; 
 var numberOfFruits : int; 
 
 function Start () 
 { 
     StartCoroutine(GenerateFruits()); 
     numberOfFruits = 20;
 }
 
 function Update() 
 {
 
 }
 
 function GenerateFruits() 
 {
     while (numberOfFruits > 0) 
     { 
         Instantiate(bluefruit); 
         yield new WaitForSeconds(3.0f); 
     }
 }

at the moment, nothing is happening! Thank you for taking the time to respond to me!

Comment
Add comment · Show 3
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 AlucardJay · Dec 09, 2012 at 06:51 AM 0
Share

I just formatted your code. You can do this by highlighting all your code, then clicking the 10101 button at the top of the edit window before posting. When you don't mark your code as code, the text gets html escaped and it gets really hard to reformat it.

I have formatted this one for you. Please check the code is the same as what you have.

To help with your problem, read and try using Invoke

http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$onoBehaviour.Invoke.html

http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$onoBehaviour.InvokeRepeating.html

for example, in your main script that Instantiates fruit, in the start function call InvokeRepeating to instantiate a fruit every 3 seconds. Then on your fruit have a separate script. On the fruit script, in the start function, Invoke with a delay of 10 seconds to a command that destroys the fruit.

avatar image Eric5h5 · Dec 09, 2012 at 07:49 AM 0
Share

@alucardj: Destroy has a timer, so there's no real reason to use Invoke for that, or a separate script.

 var fruitClone = Instantiate(bluefruit);
 Destroy(fruitClone, 10.0);
avatar image AlucardJay · Dec 09, 2012 at 07:58 AM 0
Share

true, I was keeping it simple and just directing focus to Invoke (thought Fattie would be happy to see that too).

But absolutely, my bullets have Destroy(gameObject, 10.0); in the start function. You raise an interesting point stating Destroy can be called from the instantiate script using the variable the fruit is assigned to, thanks.

2 Replies

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

Answer by fafase · Dec 09, 2012 at 08:14 AM

 pragma strict
 
 var bluefruit : GameObject; 
 var numberOfFruits : int=20; 
 
 function Start () { 
     InvokeRepeating("GenerateFruits",0.01f,3.0f); 
 }
 
 function GenerateFruits() {
     var fruit=Instantiate(bluefruit,position,rotation); 
     Destroy(fruit,10.0f) 
     if(--numberOfFruit <=0)CancelInvoke();
 }

This is how I would try it. Am I totally right? You tell me...

Comment
Add comment · 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
0

Answer by Eric5h5 · Dec 09, 2012 at 07:45 AM

Public variables take their value from the inspector. You start the coroutine, with numberOfFruits presumably set to 0 in the inspector, so the GenerateFruits function does nothing and exits. You then set numberOfFruits to 20 afterward in code, but at that point it has no effect since nothing is using that variable.

Comment
Add comment · 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

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

12 People are following this question.

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

Related Questions

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Destroy on Collision? 1 Answer

"Destroy Game Object" Not Destroying Game Object 3 Answers

Destroy GameObject Variable? 3 Answers

Erasing a gameobject if you have the coordinates. 3 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